C#调用C++写好的dll,dll导出的函数的参数是函数地址

苏克贝塔03 2014-02-12 07:41:38
C#调用C++写好的dll,dll导出的函数的参数是函数地址,
在C++里我知道怎么使用这个dll,但是在c#里我就搞不好了,麻烦大家帮我看看,
一下是C++的TestDll,请问大家在C#里面该怎么使用这个dll,
void CTEST_PORT_DLLDlg::OnOK() 
{
// TODO: Add extra validation here
Init(2,9600); //初始化串口
SetReceiveFuntion(&ReveveChar);//指定接收和处理数据的函数

//CDialog::OnOK();
}

void ReveveChar(WPARAM data, LPARAM port)//实现接收和处理数据的函数
{
static int RecevCount = 0;
char str[512] = "";
sprintf(str,"%02x",data);
strTemp += str;
RecevCount++;
if (RecevCount >= 8)
{
RecevCount = 0;
strTemp = "接收到得数据协议为:" + strTemp;
AfxGetMainWnd()->GetDlgItem(IDC_EDITRECEV)->SetWindowText(strTemp);
//AfxMessageBox(strTemp);
strTemp = "";
}
}
...全文
185 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏克贝塔03 2014-05-05
  • 打赏
  • 举报
回复
如果没有这句的话,
1 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 

C#里面只要被回调一次,程序就异常退出。返回值:0x80000003。如下: 程序“[24768] YKYDemo.vshost.exe: 托管(v4.0.30319)”已退出,返回值为 -2147483645 (0x80000003)。
苏克贝塔03 2014-05-05
  • 打赏
  • 举报
回复
C++回调C#里面的函数,我是弄通了。 C++里面用的是函数指针,但C#里面只能用委托了,并且在声明委托前要加上这句:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
包含在:
using System.Runtime.InteropServices;
太不容易了。。。 具体会不会影响C++Dll的稳定性,还要多试验。。。
mjp1234airen4385 2014-02-14
  • 打赏
  • 举报
回复
在C++回调C#函数的时候,C#里的内存回收机制,会影响C++动态库的稳定性。 也许你碰不到这个问题。
苏克贝塔03 2014-02-13
  • 打赏
  • 举报
回复
引用 2 楼 mjp1234airen4385 的回复:
你在C#里使用dllimport导入函数就行,要进行适合参数的修改,如果有回调函数声明的就麻烦一点了。
请问这个麻烦有多麻烦,我不怕麻烦?
苏克贝塔03 2014-02-13
  • 打赏
  • 举报
回复
怎么没人帮我看看呢,
mjp1234airen4385 2014-02-13
  • 打赏
  • 举报
回复
你在C#里使用dllimport导入函数就行,要进行适合参数的修改,如果有回调函数声明的就麻烦一点了。
CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧