本文介绍了我想知道如何在C#控制台上使用C ++编写的ocx。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。那里。



我创建了一个简单的C ++ ocx文件,并创建了一个HTML文件来验证ocx。



我确认ocx已在注册表中成功注册并确认ocx通常在测试HTML中被调用以启动AfxMessageBox()。



但是,我我想直接在C#中调用ocx。



为了实现ocx调用,我在Google中查找了编组技术并编写了通过编组调用ocx的C#代码。



因此,没有调用ocx,也无法确定这是C ++ ocx代码问题还是C#代码问题。



我找到了解决这个问题两周的方法,但我无法解决。



我想帮助解决这些问题。



我的尝试:



我创建ocx的过程如下:



1.创建一个MFC Ac tiveX控件Visual Studio 2015中的项目。

2.我修改了自动生成的Ctrl.cpp,Ctrl.h和.idl文件。



- ActiveXTest4_NonCtrl.cpp

Hi. There.

I created a simple C ++ ocx file, and created an HTML file to validate ocx.

I confirmed that ocx was successfully registered in the registry and confirmed that ocx was normally invoked in the test HTML to launch the AfxMessageBox().

However, I wanted to call ocx directly in C #.

To implement the ocx call, I looked up the marshaling technique in Google and wrote C # code that calls ocx through marshaling.

As a result, ocx has not been called, and there is no way to determine if this is a C ++ ocx code problem or a C # code problem.

I found a way to solve this problem over two weeks, but I could not solve it.

I would like to get help to solve these problems.

What I have tried:

The process I followed to create ocx is as follows.

1. Create an "MFC ActiveX control" project in Visual Studio 2015.
2. I modified the Ctrl.cpp, Ctrl.h, and .idl files that are automatically generated.

- ActiveXTest4_NonCtrl.cpp

BEGIN_DISPATCH_MAP(CActiveXTest4_NonCtrl, COleControl)
	DISP_FUNCTION_ID(CActiveXTest4_NonCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CLinkCtrl, "TestFunction", dispidTestFunction,TestFunction, VT_I4, VTS_I4 VTS_I4)
END_DISPATCH_MAP()

.....

void CActiveXTest4_NonCtrl::AboutBox()
{
	CDialogEx dlgAbout(IDD_ABOUTBOX_ACTIVEXTEST4_NON);
	dlgAbout.DoModal();
}

int CActiveXTest4_NonCtrl::TestFunction(int Parameter1, int Parameter2){
	CString str;
	str.Format(_T("%d"), Parameter1 + Parameter2);
	AfxMessageBox(str);
	return 1;
}



- ActiveXTest4_Non.idl


- ActiveXTest4_Non.idl

importlib(STDOLE_TLB);

	//  Basic dispatch interface of CActiveXTest4_NonCtrl.
	[ 
		uuid(3C788A60-2A80-45E8-8845-FE460E779EC7)	
	]
	dispinterface _DActiveXTest4_Non
	{
		properties:
			
		methods:
			[id(DISPID_ABOUTBOX)] void AboutBox();
			[id(1)] int TestFunction(int Parameter1, int Parameter2);
	};



- ActiveXTest4_NonCtrl.h


- ActiveXTest4_NonCtrl.h

class CActiveXTest4_NonCtrl : public COleControl
{
	DECLARE_DYNCREATE(CActiveXTest4_NonCtrl)

// Creator.
public:
	CActiveXTest4_NonCtrl();

// override.
public:
	virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
	virtual void DoPropExchange(CPropExchange* pPX);
	virtual void OnResetState();

// Implementation.
protected:
	~CActiveXTest4_NonCtrl();

	DECLARE_OLECREATE_EX(CActiveXTest4_NonCtrl)    // Class Factory and GUID
	DECLARE_OLETYPELIB(CActiveXTest4_NonCtrl)      // GetTypeInfo
	DECLARE_PROPPAGEIDS(CActiveXTest4_NonCtrl)     // property page ID.
	DECLARE_OLECTLTYPE(CActiveXTest4_NonCtrl)		

// Message Map.
	DECLARE_MESSAGE_MAP()

// Dispatch Map.
	DECLARE_DISPATCH_MAP()
	afx_msg void AboutBox();
	afx_msg int TestFunction(int Parameter1, int Parameter2);
// Event Map.
	DECLARE_EVENT_MAP()

// Dispatch and Event ID.
public:
	enum {
		dispidTestFunction = 1L,
	};
};



3.我已经构建了一个修改过的文件。

4. ocx文件已经创建并且已经成功在注册表中注册。

5.我编写了测试html文件。


3. I have built a modified file.
4. The ocx file has been created and has been successfully registered in the registry.
5. I wrote the test html file.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Test Document </TITLE>
 </HEAD>
 <BODY>

TEST
<OBJECT ID="Test" CLASSID="clsid:5CDEAFC2-43EE-48BC-8052-3895514CD062" width="0" height="0"></OBJECT>

 <script>
 	var str = document.Test.TestFunction(5, 2);
 </script>
 </BODY>
</HTML>



6.我确认ocx在html中正常执行。

7.我创建了一个C#控制台项目,用C#测试它。

8.我使用编组来加载C#中的ocx,使用编组的代码是:


6. I confirmed that ocx is executed normally in html.
7. I created a C # console project to test it in C #.
8. I used marshaling to load ocx in C #, and the code that used marshaling is:

 [ComVisible(true), ComImport,
 Guid("5CDEAFC2-43EE-48BC-8052-3895514CD062"),
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 interface IMarshal
 {
     [DispId(1)]
     int TestFunction(int param1, int param2);
 }

private static void MarshalTest()
{
   Type typeofMarshal = Type.GetTypeFromProgID("ACTIVEXTEST4_NON.ActiveXTest4_NonCtrl.1");
   if (typeofMarshal == null) return;

   var objMashal = Activator.CreateInstance(typeofMarshal);
   IMarshal iMarshal = (IMarshal)objMashal;
   if (iMarshal == null) return;

   int retVal = iMarshal.TestFunction(1, 2);
}





我的开发环境如下。



ocx要创建的项目:VS2015 C ++

项目名为ocx:VS2015,.NETFramework 4.5,C#

生成的ocx信息

- clsId =5CDEAFC2-43EE-48BC-8052-3895514CD062

- progId =ACTIVEXTEST4_NON.ActiveXTest4_NonCtrl.1

如何调用:Marshal



My development environment is as follows.

ocx Project to create: VS2015 C ++
Project called ocx: VS2015, .NETFramework 4.5, C #
Generated ocx information
- clsId = "5CDEAFC2-43EE-48BC-8052-3895514CD062"
- progId = "ACTIVEXTEST4_NON.ActiveXTest4_NonCtrl.1"
How to call: Marshal

推荐答案


这篇关于我想知道如何在C#控制台上使用C ++编写的ocx。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:37