C# 函数 传入 C++动态库中 做回调函数
?
?
MFC中調用C#寫的DLL, ? 涵數接口為char* ? proc(char* ? a,char* ? b);?我用顯式調用,定義 ? typedef ? char* ? (MYPROC)(char* ? aa,char* ? bb;每次調用后總會出現?
Run-Time ? ? Check ? Failure ? #0—The ? value ? of ? ESP ? was ? not ? properly ? saved?
across ? a ? function ? call. ? This ? is ? usually ? a ? result ? of ? calling ? a ? function?
declared ? with ? one ? calling ? convention ? with ? a ? function ? pointer ? declared?
with ? a ? different ? calling ? convention.?
有什么解決辦法?
?
現在定義 ? typedef ? char* ? (_stdcall ? MYPROC)(char* ? aa,char* ? bb)后上邊問題解決,不過傳遞參數的格式好象有點問題,MFC的char* ? ? 與c#的char*有什么不同嗎??
?
<DllTest.h>
// 任何其他項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將
// DLLTEST_API 函數視為是從 DLL 導入的,而此 DLL 則將用此宏定義的
// 符號視為是被導出的。
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif
typedef float (_stdcall Kin)(float , BYTE* ,? float? );
Kin *kin=0;
extern "C"
{
?DLLTEST_API int ndlltest;
?DLLTEST_API int fndlltest(void);
?DLLTEST_API float? kinescope(Kin kines) ;
???
?DLLTEST_API void?? kinescope1(void(*?? kines)(float?? x,?? float?? y) ) ;
?
?DLLTEST_API int SetCallBack(Kin kines);
?DLLTEST_API float TestCallBack(float a,BYTE* pData,float b);
}
?
?
?
// dlltest.cpp : 定義 DLL 應用程序的導出函數。
//
#include "stdafx.h"
#include "dlltest.h"
// 這是導出變量的一個示例
extern DLLTEST_API int ndlltest=0;
// 這是導出函數的一個示例。
DLLTEST_API int fndlltest(void)
{
?return 42;
}
DLLTEST_API float?? kinescope(Kin kines)
{
??? float a,b;
?a=1.0;b=100.0;
???? return a+b;
}
DLLTEST_API void?? kinescope1(void(*?? kines)(float?? x,?? float?? y) )
{
?kines(10,20);
}
DLLTEST_API int SetCallBack(Kin kines)
{
?kin=&kines;
?return (int)kin;
}
DLLTEST_API float TestCallBack(float a,BYTE* pdata,float b)
{
?float val=0;
?if(kin)
?{
??val=(*kin)(a,pdata,b);
?}
?return val;
}
?
?
?
C#中調用代碼
?
?
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace dlltestfrm
{
??? public partial class Form1 : Form
??? {
?
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????? }
??????? //----------------C#中調用代碼---------------??
?
??????? [DllImport(@"C:\Users\puma\Documents\Visual Studio 2008\Projects\dlltest\Debug\dlltest.dll", CharSet = CharSet.Ansi, BestFitMapping = true)]
??????? static extern int SetCallBack(IntPtr pKines);
??????? [DllImport(@"C:\Users\puma\Documents\Visual Studio 2008\Projects\dlltest\Debug\dlltest.dll", CharSet = CharSet.Ansi, BestFitMapping = true)]
??????? unsafe static extern float TestCallBack(float a,byte* pdata,float b);
??????? //------------委托聲明-------------
??????? unsafe public delegate float kinds(float x,byte* pdata ,float y);
??????? public static byte[] pBuf = new byte[10];
??????? public static int BufLen = 0;
??????? //---------------委托定義-------------------
??????? unsafe public?? static?? float?? kds(float?? x, byte* pdata,? float?? y)
??????? {
??????????? int i,j,len = (int)y;
??????????? for (i = 0,j=0; i < len; i++)
??????????? {
??????????????? j +=(int) pdata[i];
??????????? }
??????????? return (float)(x+j);
??????? }
??????? kinds kk;
??????? IntPtr pKinds;
??????? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? //------------------調用---------------
??????????? unsafe
??????????? {
??????????????? kk = new kinds(kds);
??????????????? pKinds = Marshal.GetFunctionPointerForDelegate(kk);
??????????????? int pt = SetCallBack(pKinds);
??????????? }
??????? }
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? byte[] pd = { 1, 2, 3, 4 };
??????????? unsafe
??????????? {
??????????????? fixed (byte* pp = pd)
??????????????? {
??????????????????? label1.Text = TestCallBack(float.Parse(textBox1.Text), pp, 4.0f).ToString();
??????????????? }
??????????? }
??????? }
???????
??? }
}
?
?
?
?
轉載于:https://www.cnblogs.com/pumax/archive/2010/09/22/1833205.html
總結
以上是生活随笔為你收集整理的C# 函数 传入 C++动态库中 做回调函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最新的Windows phone Dev
- 下一篇: c# char unsigned_dll