//将汉字转换为内码//Function SC_NeiMa(pHanZi:pchar):Pchar;StdCall; 原型函数
[DllImport("SCDLL32.dll", EntryPoint = "SC_NeiMa", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe string SC_NeiMa(string pc);
//调用
textBox8.Text = SC_NeiMa(textBox1.Text.Trim());
为何?
[DllImport("SCDLL32.dll", EntryPoint = "SC_NeiMa", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe string SC_NeiMa(string pc);
//调用
textBox8.Text = SC_NeiMa(textBox1.Text.Trim());
为何?
public static extern unsafe string SC_NeiMa(string pc);
[DllImport("KERNEL32", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);
[DllImport("SCDLL32.dll", EntryPoint = "SC_NeiMa", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe string SC_NeiMa([MarshalAs(UnmanagedType.LPWStr)] String pbstr)
内存的.所以返回的一般都是你传入的指针.如果会的话改用StringBuilder.另外...C写的?这很明显的Pascal吧囧.
你先试试这样
[DllImport("SCDLL32.dll", EntryPoint = "SC_NeiMa", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern string SC_NeiMa([MarshalAs(UnmanagedType.LPWStr)] String pbstr);
如果不行,试试这样(因为PChar是一个指针)[DllImport("SCDLL32.dll", EntryPoint = "SC_NeiMa", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SC_NeiMa(IntPtr pbstr);//调用:string str = "aaa";
IntPtr poniter = Marshal.StringToHGlobalUni(str); //获取指针if(pointer != IntPtr.Zero)
{
IntPtr returnPtr = SC_NeiMa(pointer);
string returnString = Marshal.PtrToStringUni(returnPtr); //把指针转回Unicode String
}
IntPtr pointer = Marshal.StringToHGlobalUni(str); //获取指针
基本可肯定是这里错了;Cdecl调用返回后要恢复堆栈.
而实际上stdcall内部已经恢复了堆栈.这里会导致堆栈的错误.
[DllImport("SCDLL32.dll", EntryPoint="SC_NeiMa", SetLastError=true, CharSet= CharSet.Ansi, CallingConvention= CallingConvention.StdCall)]
public static extern string SC_NeiMa([MarshalAs(UnmanagedType.LPStr)] String pbstr);
如果不行,试试这样(因为PChar是一个指针)[DllImport("SCDLL32.dll", EntryPoint="SC_NeiMa", SetLastError=true, CharSet= CharSet.Auto, CallingConvention= CallingConvention.StdCall)]
public static extern IntPtr SC_NeiMa(IntPtr pbstr);//调用:
string str = "aaa";
IntPtr poniter = Marshal.StringToHGlobalAnsi(str);//获取指针if(pointer!= IntPtr.Zero)
{
IntPtr returnPtr = SC_NeiMa(pointer);
string returnString= Marshal.PtrToStringAnsi(returnPtr);//把指针转回Ansi String
}
我已将dll发给你,谢谢帮我看看!