C ++ DLL与C#应用程序之间的通信 [英] Communication between C++ DLL and C# Applicaiton

查看:101
本文介绍了C ++ DLL与C#应用程序之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.Net Compact Framework 2

我有一个C ++ DLL,它实现了STDAPI DllGetClassObject(REFCLSID rclsid,REFIID riid,void ** ppObject)
,我也有一个C#应用程序实现MessageWindow,以在每次调用DllGetClassObject时侦听来自C ++ DLL的事件.

首先,我试图传递一个指向SendMessage的LPARAM参数的指针(在C ++中),但这是成功的,当C#收到它时,它无法正确转换/广播.
例如

Using .Net Compact Framework 2

I have a C++ DLL which implements STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void ** ppObject)
and I also have a C# application which implements MessageWindow to listen for an event from the C++ DLL whenever DllGetClassObject gets called.

At first I was trying to pass a pointer to the LPARAM parameter of SendMessage (in C++), this was successful, however, when C# receives it it can't convert/cast it properly.
e.g

<C++>
int* x = new int;
*x = 99;
SendMessasge(hwnd, WM_ACTIVATE, 0, (LPARAM)x);


<C#, using unsafe code>
int* x = (int*) m.LParam;



因此,我尝试使用共享内存...但是,这仍然成为问题.

我感谢大家的帮助.
/>
谢谢.

This causes the application to crash with no exception.

Thus I tried using shared memory...however, this still becomes the problem.

I appreciate everyone's help.

Thanks.

推荐答案

你好,

我不明白为什么不使用DLLImport呢?它比使用不安全的代码更好,更安全.如果您有兴趣,可以参考
DllImportAttribute DLLImport.
感谢
Chao

Hello,

I do not understand why not you use DLLImport instead. It is much better and safe than using unsafe code. You could reference
DllImportAttribute, if you have interest in DLLImport.
Thanks
Chao


这篇关于C ++ DLL与C#应用程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆