C#調(diào)用C類型dll入?yún)閟truct的問題詳解
前言
C# 可以通過 DllImport 的方式引用 C 類型的 dll。但很多 dll 的參數(shù)不會是簡單的基礎(chǔ)類型,而是結(jié)構(gòu)體 struct 。因此就需要在 C# 端定義同樣的結(jié)構(gòu)體類型,才能實(shí)現(xiàn)調(diào)用 C 類型 dll。這里例舉幾種不同的結(jié)構(gòu)體情況,以及其對應(yīng)的解決方案。
基礎(chǔ)調(diào)用方式
對于一個(gè)結(jié)構(gòu)體類型:
typedef struct DATA
{
int nNumber;
float fDecimal;
};
在 C# 端就需要定義為
[StructLayout(LayoutKind.Sequential)]
public struct DATA
{
public int nNumber;
public float fDecimal;
}
包含字符數(shù)組
對于一個(gè)包含字符數(shù)組的結(jié)構(gòu)體類型:
typedef struct DATA
{
int nNumber;
float fDecimal;
char szString[256];
};
在 C# 端就需要使用 Marshal 設(shè)置數(shù)據(jù)空間大小,同時(shí)最好定義一個(gè)初始化函數(shù)與 get 的定義
[StructLayout(LayoutKind.Sequential)]
public struct DATA
{
void alloc() {
szString = new char[256];
}
string sString {
get {
int nLength = 256;
string sData = "";
for (int i = 0; i < nLength; i++)
{
if (szData[i] == '\0') break;
sData += szData[i];
}
return sData;
}
}
public int nNumber;
public float fDecimal;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
char[] szString;
}
包含字符二維數(shù)組
對于一個(gè)包含字符二維數(shù)組的結(jié)構(gòu)體類型:
typedef struct DATA
{
int nNumber;
float fDecimal;
char szString[6][256];
};
在 C# 端同樣需要使用 Marshal 設(shè)置數(shù)據(jù)空間大小,需要將兩個(gè) Size 相乘,并定義一個(gè)初始化函數(shù)。同時(shí)在做一個(gè) get 的定義。
[StructLayout(LayoutKind.Sequential)]
public struct DATA
{
void alloc() {
szString = new char[256 * 6];
}
public string[] sStrings
{
get {
int nSize = 6, nLength = 256;
string[] sDatas = new string[nSize];
for (int i = 0; i < nSize; i++)
{
for (int j = 0; j < nLength; j++)
{
if (szData[i * nLength + j] == '\0') break;
sData += szData[i * nLength + i];
}
sDatas[i] = sData;
}
return sDatas;
}
}
public int nNumber;
public float fDecimal;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256 * 6)]
char[] szStrings;
}
dll 入?yún)榻Y(jié)構(gòu)體數(shù)組
若有一個(gè)這樣的 C dll 函數(shù)定義:
void FnCall(DATA* datas); // 調(diào)用方式 DATA datas[10]; fnCall(datas);
那么,在 C# 中要實(shí)現(xiàn)等價(jià)調(diào)用:
// 首先 Import 函數(shù)
[DllImport("Module.dll")]
public static extern void FnCall(IntPtr pInfo); // 注意入?yún)⒁x為指針
// 再定義定義結(jié)構(gòu)體數(shù)組
int nCount = 10;
DATA datas = new DATA[nCount];
// 再分配內(nèi)存空間
int nSize = Marshal.SizeOf(typeof(DEVICE_INFO));
IntPtr Dataptr = Marshal.AllocHGlobal(nSize * nCount);
// 調(diào)用函數(shù)
FnCall(Dataptr);
// 復(fù)制數(shù)據(jù)到結(jié)構(gòu)體中
for (int i = 0; i < nCount; i++)
{
IntPtr ptr = (IntPtr)((UInt32)Dataptr + i * size);
datas[i] = (DEVICE_INFO)Marshal.PtrToStructure(ptr, typeof(DEVICE_INFO));
}
// 釋放內(nèi)存空間
Marshal.FreeHGlobal(Dataptr);
另外,如果你要調(diào)用的 dll 是非 C 類型 dll,而是 C++ Class。那么我們就可以將其再包裝一層,轉(zhuǎn)換為 C 類型 dll。
例如:
class Example {
public:
int MethodCall();
};
那么就可以編寫 C 類型的 dll。
extern "C" {
Example* Example_New() {
return new Example();
}
int Example_MethodCall(Example* p) {
return p->MethodCall();
}
void Example_Delete(Example* p) {
delete p;
}
}
C# 那邊就這樣導(dǎo)入
[DllImport("Module.dll")]
public static extern IntPtr Example_Create();
[DllImport("Module.dll")]
public static extern int Example_MethodCall(IntPtr value);
[DllImport("Module.dll")]
public static extern void Example_Delete(IntPtr value);
// 調(diào)用方式
IntPtr p = Example_Create();
Example_MethodCall(p);
Example_Delete(p);
至于 C 類型 dll 中其他類型變量在 C# 的對應(yīng),則可以參考 Microsoft 的 文檔 。
總結(jié)
到此這篇關(guān)于C#調(diào)用C類型dll入?yún)閟truct問題的文章就介紹到這了,更多相關(guān)C#調(diào)用C類型dll入?yún)?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解C#索引器(一種支持參數(shù)的屬性)與屬性的對比
本篇文章是對C#索引器(一種支持參數(shù)的屬性)與屬性的對比進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
如何用C#在PC上查找連接藍(lán)牙設(shè)備并實(shí)現(xiàn)數(shù)據(jù)傳輸
這篇文章主要介紹了如何用C#在PC上查找連接藍(lán)牙設(shè)備并實(shí)現(xiàn)數(shù)據(jù)傳輸,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#使用Objects?Comparer進(jìn)行對象比較
本文主要介紹了C#使用Objects?Comparer進(jìn)行對象比較,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

