DllImport.net frameworksdk documentation -> reference -> Compiler and Language Reference -> C# -> C# programmer's Reference -> C# tutorials -> platform invoke tutorials
解决方案 »
- 用C#调用有道词典,实现文章翻译
- 关于C#调用第三方控件时遇到的问题
- MicrosoftReportViewer的问题!
- 在c#应用程序中实现从Excel文件导入数据,但是为什么每次导入都会多出三空白行的
- 第2次查询的时候,listview为何不显示数据
- PowerDesigner创建自动编号列的问题
- 请教高手,要做一个类似Flash安排脚本的多个进度条的控件怎么做?
- 在webclient中的url中如果含有中文,则结果不对,应该如何设置编码
- 活动目录编程问题,解决到差不多了,就差一点了~~~~~
- 请问:DataGrid控件可以对dbf文件访问吗?
- 关于IIS一问
- 安装的中文版vs.net,调试c#程序时,提示不能调用类库,为什么?
具体请看C# interop的解析。是关于如何工作于
unsafe code
我还真不知道C#怎么Marshal CString呢:),要真像这样做还得费不少功夫呢!
我的建议是:先用VC++做一层包装。这个Wrap的工作就是把CString的参数和返回值转换成标准的const char*。然后把这个Wrap也编译成Dll,从C#中直接用这个Wrap的DLL。这样要容易得多。
谢谢。
我也有这种想法。
不过,不知可不可以直接在C#进行调用?
我试了一下,好像直接在c#中使用string就可以了.
关键是: int *怎么处理。
它通过int *来传出函数的执行情况,并据此判断、处理?
谢谢!!!
string FunSys(ref int a, string b);
string FunSys(out int a, string b); //急得调用的时候也要加ref/out其次,用unsafe code,是可以取得int*的,所以可以直接用int*声明。
最后,Marshal.HGlobalAlloc可以在GlobalHeap中申请内存,返回IntPtr,相当于int*,所以也可以这样写:
string FunSys(IntPtr pa, string b); //用完记得释放。
请指教,谢谢!
------------
An unhandled exception of type 'System.NullReferenceException' occurred in CallAPI2.exe
Additional information: Object reference not set to an instance of an object.
------------
源代码:
private void button1_Click(object sender, System.EventArgs e)
{
string a="aaa";
string strResult="";
int i=0;
strResult = FunSys(ref i, a);
}
[DllImport("DllName.dll",CharSet=CharSet.Auto)]
public static extern string FunSys(
ref int para1,
string para2
);
Cstring的情况:
eax --> CString Object(LPSTR) ---> 实际字符串
const char*/LPSTR的情况:
eax --> 实际字符串
谢谢!
但如果你执意要用CString的话,可以试试这样:CString在C#中的定义是:
[StructLayout(LayoutKind.Sequential)]
public struct CSString {
[MarshalAs(UnmanagedType.LPTStr)]
string _str;
}
可以把函数的返回这说明成为CSString的形式:
public static extern CSString FunSys(
ref int para1,
string para2
);
也许可以。
按照你提供的方法,出现如下错误信息:
An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in CallAPI2.exeAdditional information: Method's type signature is not PInvoke compatible.
--------------
谢谢。
还是加一层包装吧。
请问如何在vc6.0中添加一层包装。
现在只提供了DLL和lib文件。
谢谢!
谢谢各位高手!
写在你的调用函数前[System.Runtime.InteropServices.DllImportAttribute("filename.dll")]
public static extern CString FunSys(int * par1, CString par2);
C#中并没有CString类呀!!!
还是说 怎样在VC6.0中如何调用吧!
谢谢
#include <TCHAR.H>void FunSysWrap(int* p, LPTSTR pstr, LPTSTR buffer) {
CString s(pstr);
CString ret = FunSys(p, s);
LPTSTR temp = ret.LockBuffer();
_tcscpy(buffer, temp);
return;
} 然后C#里面调用FunSysWrap(ref int, string, StringBuilder)就可以了。至于怎么用VC生成DLL,参考MSDN的例子吧,记得把.lib包含到Project的link里面就可以了。
下面是整个代码,很难解释,就将就着用把:
DLL:
CString FunSys(int* p, CString str);C#:
[StructLayout(LayoutKind.Sequential)]
public struct MyString {
public IntPtr _str;
} [DllImport("mydll.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public extern static void DllFun(IntPtr pt, ref int i, string str);
//千万别问我为什么:)调用:
int i = 0;
IntPtr mem = Marshal.AllocHGlobal(64); //申请多点内存,先。
DllFun(mem, ref i, "hello, world");
MyString mystr = (MyString)Marshal.PtrToStructure(mem, typeof(MyString));
string str = Marshal.PtrToStringAnsi(mystr._str);
Console.WriteLine(str);
Marshal.FreeHGlobal(mem);
...真是麻烦,乖乖妹要请客的说!:)