C#如何调用C++编写的结构类型的dll?

haikuotiankong 2007-04-05 10:38:32
C++ DLL中的结构定义:
typedef struct {
bool bIsInfo;
char strANI[256];
char strDNIS[256];
char strOriginANI[256];
char strOriginDNIS[256];
char strFromDN[256];
char strToDN[256];
char strDestAgentID[256];
char strSkillGroup[256];
}AgentCallInfo;

C++ DLL中的接口函数定义:
AgentCallInfo* GetAgentCallInfo()
{
AgentCallInfo g_CallInfo;
...
return (&g_CallInfo);
}



我在C#中对结构的定义:
[ StructLayout( LayoutKind.Sequential )]
public struct AgentCallInfo
{
public bool bIsInfo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strANI;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strDNIS;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strOriginANI;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strOriginDNIS;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strFromDN;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strToDN;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strDestAgentID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strSkillGroup;
}

对接口的声明:
[DllImport("DesktopAgent.dll", CharSet = CharSet.Ansi)]
public static extern AgentCallInfo GetAgentCallInfo();

使用:
AgentCallInfo agentCallInfo =GetAgentCallInfo();


结果报错。偶定义的结构对吗?(偶是菜鸟 :( )应该怎样调用这个DLL接口呢?请高手指点,谢谢!
...全文
294 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxelement 2007-04-05
  • 打赏
  • 举报
回复
而且C++,C#中的long是不同的。
haikuotiankong 2007-04-05
  • 打赏
  • 举报
回复
那我到底错在哪儿呢?! :(
Little_Ghost 2007-04-05
  • 打赏
  • 举报
回复
mark
Avoid 2007-04-05
  • 打赏
  • 举报
回复
上面说的很详细了,有一点你要记住,C++中BOOL与bool在c#中是不同的,c++中bool只有一位.所以直接用c#中的bool就是错的.c#的bool是四位的,与BOOL等价.对于bool可以用char来代替.
zhangliu_521 2007-04-05
  • 打赏
  • 举报
回复
为了能用上原来的C++代码,只好研究下从C# 中调用DLL
首先必须要有一个声明,使用的是DllImport关键字:
包含DllImport所在的名字空间
using System.Runtime.InteropServices;
public class XXXX{

[DllImport(“MyDLL.dll")]
public static extern int mySum (int a,int b);
}


[DllImport(“MyDLL.dll")]
public static extern int mySum (int a,int b);
代码中DllImport关键字作用是告诉编译器入口点在哪里,并将打包函数捆绑在这个类中
在调用的时候
在类中的时候 直接  mySum(a,b);就可以了
在其他类中调用: XXXX. mySum(a,b);

[DllImport(“MyDLL.dll”)]在申明的时候还可以添加几个属性
[DllImport(“MyDLL.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)
]
EntryPoint: 指定要调用的 DLL 入口点。默认入口点名称是托管方法的名称 。
CharSet: 控制名称重整和封送 String 参数的方式 (默认是UNICODE)
CallingConvention指示入口点的函数调用约定(默认WINAPI)(上次报告讲过的)
SetLastError 指示被调用方在从属性化方法返回之前是否调用 SetLastError Win32 API 函数 (C#中默认false )


int 类型
[DllImport(“MyDLL.dll")]
//返回个int 类型
public static extern int mySum (int a1,int b1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(int a2,int b2)
{
//a2 b2不能改变a1 b1
//a2=..
//b2=...
return a+b;
}


//参数传递int 类型
public static extern int mySum (ref int a1,ref int b1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(int *a2,int *b2)
{
//可以改变 a1, b1
*a2=...
*b2=...
return a+b;
}


DLL 需传入char *类型
[DllImport(“MyDLL.dll")]
//传入值
public static extern int mySum (string astr1,string bstr1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(char * astr2,char * bstr2)
{
//改变astr2 bstr 2 ,astr1 bstr1不会被改变
return a+b;
}


DLL 需传出char *类型
[DllImport(“MyDLL.dll")]
// 传出值
public static extern int mySum (StringBuilder abuf, StringBuilder bbuf );
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(char * astr,char * bstr)
{
//传出char * 改变astr bstr -->abuf, bbuf可以被改变
return a+b;
}

DLL 回调函数

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)



using System;
using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam); //定义委托函数类型
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main() {
CallBack myCallBack = new CallBack(EnumReportApp.Report); EnumWindows(myCallBack, 0);
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd); return true;
}
}


DLL 传递结构
BOOL PtInRect(const RECT *lprc, POINT pt);

using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Point {
public int x;
public int y;
}
[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
}
Class XXXX {
[DllImport("User32.dll")]
public static extern bool PtInRect(ref Rect r, Point p);
}

jinanjiang 2007-04-05
  • 打赏
  • 举报
回复
直接引用不行吗?
haikuotiankong 2007-04-05
  • 打赏
  • 举报
回复
不好意思,我概念不清。。。先别晕,能告诉我怎么调用这个接口吗?谢谢
Red_angelX 2007-04-05
  • 打赏
  • 举报
回复
GetAgentCallInfo返回的是一个结构体,而不是一个结构体指针.返回值的类型是结构体
--------------------
晕-_-!
Red_angelX 2007-04-05
  • 打赏
  • 举报
回复
它返回的是个地址
haikuotiankong 2007-04-05
  • 打赏
  • 举报
回复
GetAgentCallInfo返回的是一个结构体,而不是一个结构体指针.返回值的类型是结构体(它代表一块非托管内存)的情况我从未遇见过,我在msdn上查到的资料,也没说如何处理这种情况,我也感到很困惑.
Red_angelX 2007-04-05
  • 打赏
  • 举报
回复
long有什么不同?一样用
AgentCallInfo* GetAgentCallInfo()
返回的是一个指针对象和AgentCallInfo GetAgentCallInfo();不同的

110,552

社区成员

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

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

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