c++.net调用c#类库
2008-01-15 09:51
369 查看
参考: http://www.codeproject.com/csharp/com_object_in_c_.asp
1.直接调用
//#using "../debug/CSLib.dll" 等于在工程中添加引用,addrefernce
using namespace ClassLibrary2;
ClassLibrary2::Form1^ xxForm = gcnew Form1();
2.c#写的DLL做成COM,生成的tlb供c++。net调用;
namespace ClassLibraryCCharp
{
[Guid("40699C45-4741-4507-971E-E0A32488CE42")]
//[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface InterfaceTest
{
void SetParentHandler(Int32 nParentHandler);
Int32 GetWindowHandler();
void CreateForm(Int32 nParentHandler);
void MoveFormSize(int x, int y, int nHeght, int nWidth);
void CloseWindow();
}
}
namespace ClassLibraryCCharp
{
public class TestImp : InterfaceTest
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetParent(int hWndChild, int hWndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetParent(int hWndChild);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool MoveWindow(int hWnd, int x, int y, int width, int height, bool bRepait);
public TestImp()
{
}
private Form _xxForm = null;
public void SetParentHandler(Int32 nParentHandler)
{
}
public Int32 GetWindowHandler()
{
if (_xxForm != null)
return _xxForm.Handle.ToInt32();
return 0;
}
public void CreateForm(Int32 nParentHandler)
{
_xxForm = new AForm();
SetParent(_xxForm.Handle.ToInt32(), nParentHandler);
_xxForm.Show(new ExternWindow(nParentHandler));
}
public void MoveFormSize(int x, int y, int nHeght, int nWidth)
{
if (_xxForm != null)
{
_xxForm.Left = 0;
_xxForm.Top = 0;
_xxForm.Width = nWidth;
_xxForm.Height = nHeght;
}
}
public void CloseWindow()
{
if (_xxForm != null)
{
_xxForm.Close();
}
}
}
sealed class ExternWindow : IWin32Window
{
IntPtr m_Hanler;
public ExternWindow(long nHandler)
{
m_Hanler = (IntPtr)(nHandler);
}
public IntPtr Handle
{
get { return m_Hanler; }
set { m_Hanler = value; }
}
}
}
注册为com interrop。
必须生成强程序集名称
c++调用
#import "../ClassLibraryCCharp/bin/debug/ClassLibraryCCharp.tlb" raw_interfaces_only
using namespace ClassLibraryCCharp;
CoInitialize(NULL);
InterfaceTestPtr pTest(__uuidof(TestImp));
pTest->CreateForm((long)hWnd);
pTest->MoveFormSize(0, 0, 100,100);
long xxFormHanlder = 0;
pTest->GetWindowHandler(&xxFormHanlder);
CoUninitialize();
1.直接调用
//#using "../debug/CSLib.dll" 等于在工程中添加引用,addrefernce
using namespace ClassLibrary2;
ClassLibrary2::Form1^ xxForm = gcnew Form1();
2.c#写的DLL做成COM,生成的tlb供c++。net调用;
namespace ClassLibraryCCharp
{
[Guid("40699C45-4741-4507-971E-E0A32488CE42")]
//[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface InterfaceTest
{
void SetParentHandler(Int32 nParentHandler);
Int32 GetWindowHandler();
void CreateForm(Int32 nParentHandler);
void MoveFormSize(int x, int y, int nHeght, int nWidth);
void CloseWindow();
}
}
namespace ClassLibraryCCharp
{
public class TestImp : InterfaceTest
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetParent(int hWndChild, int hWndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetParent(int hWndChild);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool MoveWindow(int hWnd, int x, int y, int width, int height, bool bRepait);
public TestImp()
{
}
private Form _xxForm = null;
public void SetParentHandler(Int32 nParentHandler)
{
}
public Int32 GetWindowHandler()
{
if (_xxForm != null)
return _xxForm.Handle.ToInt32();
return 0;
}
public void CreateForm(Int32 nParentHandler)
{
_xxForm = new AForm();
SetParent(_xxForm.Handle.ToInt32(), nParentHandler);
_xxForm.Show(new ExternWindow(nParentHandler));
}
public void MoveFormSize(int x, int y, int nHeght, int nWidth)
{
if (_xxForm != null)
{
_xxForm.Left = 0;
_xxForm.Top = 0;
_xxForm.Width = nWidth;
_xxForm.Height = nHeght;
}
}
public void CloseWindow()
{
if (_xxForm != null)
{
_xxForm.Close();
}
}
}
sealed class ExternWindow : IWin32Window
{
IntPtr m_Hanler;
public ExternWindow(long nHandler)
{
m_Hanler = (IntPtr)(nHandler);
}
public IntPtr Handle
{
get { return m_Hanler; }
set { m_Hanler = value; }
}
}
}
注册为com interrop。
必须生成强程序集名称
//sn -k Database_COM_Key.snk //[assembly: AssemblyKeyFile("Database_COM_Key.snk")]
c++调用
#import "../ClassLibraryCCharp/bin/debug/ClassLibraryCCharp.tlb" raw_interfaces_only
using namespace ClassLibraryCCharp;
CoInitialize(NULL);
InterfaceTestPtr pTest(__uuidof(TestImp));
pTest->CreateForm((long)hWnd);
pTest->MoveFormSize(0, 0, 100,100);
long xxFormHanlder = 0;
pTest->GetWindowHandler(&xxFormHanlder);
CoUninitialize();
相关文章推荐
- 本地C++如何调用C#类库
- 本地C++如何调用C#类库
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- 关于C++调用C#语言DLL(类库)
- C# .net写的程序调用C++.net写的动态库,装载失败
- C++调用C#dll类库中的方法(非显性COM)
- C++调用C#类库 && 非托管C++字符串和托管字符串的转换
- asp.net web c# 调用 c++ dll库 c++转c#对象错误 海康SDK
- C# 中调用C++ DLL (P/Invoke) - C++ - 海洋博客C++,net,tag,
- C++调用C#类库; 非托管C++字符串和托管字符串的转换
- C#调用C++的DLL类库
- C# 调用C++ CLR dll类库时,实现从 string 到 sbyte* 的转换
- 转:使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
- C# .net写的程序调用C++.net写的动态库装载失败,原因一句说清
- asp.net(C#)调用C++程序并交互操作
- 使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- asp.net(C#)调用C++程序并交互操作【转】http://www.cnblogs.com/greatverve/archive/2010/06/07/Csharp-transfer-Cpp.html
- C#反射之Assembly---CLR/c++可以通过反射调用c#类库