高分求救 懂C# 调用C++Dll的请进,
C++ 调用Dll的方法如下:
void CCallDllDlg::Find()
{
int cx,cy;//找到图片的横坐标cx和纵坐标cy
CString temp_x,temp_y;//
CString msg="Pee_Shangx.bmp";//要查找的bmp文件(24位色位图)
HINSTANCE hDllInst = LoadLibrary("FindPic.dll");
if(hDllInst)
{
typedef void (*MYFUNC)(CString,int*,int*);
MYFUNC youFuntionNameAlias = NULL; // youFuntionNameAlias 函数别名
youFuntionNameAlias = (MYFUNC)GetProcAddress(hDllInst,"FindPic");
// youFuntionName 在DLL中声明的函数名
if(youFuntionNameAlias)
{
youFuntionNameAlias(msg,&cx,&cy);
temp_x.Format("%d",cx);
temp_y.Format("%d",cy);
AfxMessageBox("横坐标"+temp_x+" 纵坐标"+temp_y);
SetCursorPos(cx,cy);//移动鼠标到图片位置
}
FreeLibrary(hDllInst);
}
下面是C# 调用的代码
private int iHwnd = 0; //DLL句柄
private delegate void FindPictures(StringBuilder strFileName, ref int iX, ref int iY);
private FindPictures fp;
private const string DLLNAEM = "FindPic.dll";
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProceName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary", SetLastError = true)]
public static extern bool FreeLibrary(IntPtr hModule);
public static Delegate GetAddress(IntPtr dllModule, string functionname, Type t)
{
IntPtr addr = GetProcAddress(dllModule, functionname);
if (addr == IntPtr.Zero)
return null;
else
return Marshal.GetDelegateForFunctionPointer(addr, t);
}
public void FindPicFunction(StringBuilder strName, ref int iX, ref int iY)
{
try
{
ipt = LoadLibrary(DLLNAEM);
if (ipt == IntPtr.Zero)
return;
fp = (FindPictures)GetAddress(ipt, "FindPic", typeof(FindPictures));
fp(strName, ref iX, ref iY);//这里就提示出错.说类型错误.//------------------------------------
}
catch
{
//出错
}
finally
{
FreeLibrary(ipt);
}
}
到标记的地方出错.错误代码如下
对 PInvoke 函数“#!#.Control.FindPicClass+FindPictures::Invoke”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
问题: 请问这个CString 到底应该和C# 的那种类型相对应.? (String是尝试过了.不行 StringBuilder也不行.
)
------解决方案--------------------
CString是mfc的,c#用不了,如果你不能修改dll,就不要用了,想起他办法
------解决方案--------------------
http://www.dreamdu.com/blog/2008/04/25/cs_cpp_functionpointer/