C#-从动态库dll调用函数
我必须使用C#访问动态库。 它在使用COM库时效果很好,但是当我尝试使用动态库时,它会导致错误 第一个问题C#-从动态库dll调用函数,c#,dll,C#,Dll,我必须使用C#访问动态库。 它在使用COM库时效果很好,但是当我尝试使用动态库时,它会导致错误 第一个问题 [DllImport("mydll.dll")] public static extern int toOpen(uint id, char* name); public static void Main() { char name; toOpen(0, name); } 首先,我的代码如下所示: [DllImport("mydll.dll")] public static ex
[DllImport("mydll.dll")]
public static extern int toOpen(uint id, char* name);
public static void Main()
{
char name;
toOpen(0, name);
}
首先,我的代码如下所示:
[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, char[] strVolume, char[] strInfo);
// strVolume and strInfo is parameter that return value with [out]
public static void Main()
{
char[] test1,test2;
toGetInfo(0,test1,test2);
}
[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, out char[] strVolume, out char[] strInfo);
// strVolume and strInfo is parameter that return [out]
public static void Main()
{
char[] test1,test2;
toGetInfo(0, out test1, out test2);
}
但它无法编译,因为在test1和test2中错误地使用了未分配的局部变量。
然后我通过添加out编辑代码,如下所示:
[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, char[] strVolume, char[] strInfo);
// strVolume and strInfo is parameter that return value with [out]
public static void Main()
{
char[] test1,test2;
toGetInfo(0,test1,test2);
}
[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, out char[] strVolume, out char[] strInfo);
// strVolume and strInfo is parameter that return [out]
public static void Main()
{
char[] test1,test2;
toGetInfo(0, out test1, out test2);
}
它可以编译,但返回空值给test1和test2
第二个问题
[DllImport("mydll.dll")]
public static extern int toOpen(uint id, char* name);
public static void Main()
{
char name;
toOpen(0, name);
}
编译时给出错误“指针和固定大小的缓冲区只能在不安全的上下文中使用”
你知道怎么做吗?试试下面的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication74
{
class Program
{
[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, IntPtr strVolume, IntPtr strInfo);
[DllImport("mydll.dll")]
public static extern int toOpen(uint id, IntPtr name);
const int STRING_LENGTH = 256;
static void Main(string[] args)
{
IntPtr strVolumePtr = Marshal.AllocHGlobal(STRING_LENGTH);
IntPtr strInfoPtr = Marshal.AllocHGlobal(STRING_LENGTH);
uint id = 123;
int status1 = toGetInfo(id, strVolumePtr, strInfoPtr);
string strVolume = Marshal.PtrToStringAnsi(strVolumePtr);
string strInfo = Marshal.PtrToStringAnsi(strInfoPtr);
string name = "John";
IntPtr openNamePtr = Marshal.StringToHGlobalAnsi(name);
int status2 = toOpen(id, openNamePtr);
Marshal.FreeHGlobal(strVolumePtr);
Marshal.FreeHGlobal(strInfoPtr);
Marshal.FreeHGlobal(openNamePtr);
}
}
}
字符在c中是两个字节,在c中可能是一个字节。在c#中使用一个字节[]。在c语言中,字符数组以“\0”结尾,因此最好使用IntPtr Marshal.StringToHGlobalAnsi(字符串),它会自动完成所有工作。因此,将char[]声明为IntPtr。在更改为IntPtr后,它会给出一些东西。等等,我会更新我想问,如果我不确定字符串的长度,有没有遇到这种情况的技巧?谢谢,它也适用于dword和字节吗?您需要为字符串分配内存,因此它必须比实际字符串长。c语言代码应指定此值。当您有一个int16时,只需使用out int16即可。只有指针才需要分配一个通常是数组和结构的大小。