现在的位置: 首页 > 综合 > 正文

C#调用DLL文件时参数对应表

2013年01月04日 ⁄ 综合 ⁄ 共 3635字 ⁄ 字号 评论关闭

C#调用DLL文件时参数对应表

Wtypes.h 中的非托管类型

非托管 C 语言类型

托管类名

说明

HANDLE

void*

System.IntPtr

32 位

BYTE

unsigned char

System.Byte

8 位

SHORT

short

System.Int16

16 位

WORD

unsigned short

System.UInt16

16 位

INT

int

System.Int32

32 位

UINT

unsigned int

System.UInt32

32 位

LONG

long

System.Int32

32 位

BOOL

long

System.Int32

32 位

DWORD

unsigned long

System.UInt32

32 位

ULONG

unsigned long

System.UInt32

32 位

CHAR

char

System.Char

用 ANSI 修饰。

LPSTR

char*

System.String 或 System.StringBuilder

用 ANSI 修饰。

CharSet = CharSet.Unicode

LPCSTR

Const char*

System.String 或 System.StringBuilder

用 ANSI 修饰。

LPWSTR

wchar_t*

System.String 或 System.StringBuilder

用 Unicode 修饰。

[DllImport(@"c:\FileText.dll", EntryPoint = "getText", SetLastError = false, CharSet = CharSet.Unicode)]
        public static extern String getText(String filename);

LPCWSTR

Const wchar_t*

System.String 或 System.StringBuilder

用 Unicode 修饰。

FLOAT

Float

System.Single

32 位

DOUBLE

Double

System.Double

64 位

类别

类名

说明

Visual Basic 数据类型

C# 数据类型

C++ 托管扩展数据类型

JScript 数据类型

整数

Byte

8 位的无符号整数。

Byte

byte

char

Byte

  

SByte

8 位的有符号整数。

不符合 CLS。

SByte

无内置类型。

sbyte

signed char

SByte

  

Int16

16 位的有符号整数。

Short

short

short

short

  

Int32

32 位的有符号整数。

Integer

int

int

- 或 -

long

int

  

Int64

64 位的有符号整数。

Long

long

__int64

long

  

UInt16

16 位的无符号整数。

不符合 CLS。

UInt16

无内置类型。

ushort

unsigned short

UInt16

  

UInt32

32 位的无符号整数。

不符合 CLS。

UInt32

无内置类型。

uint

unsigned int

- 或 -

unsigned long

UInt32

  

UInt64

64 位的无符号整数。

不符合 CLS。

UInt64

无内置类型。

ulong

unsigned __int64

UInt64

浮点

Single

单精度(32 位)浮点数字。

Single

float

float

float

  

Double

双精度(64 位)浮点数字。

Double

double

double

double

逻辑

Boolean

布尔值(真或假)。

Boolean

bool

bool

bool

其他

Char

Unicode(16 位)字符。

Char

char

wchar_t

char

  

Decimal

96 位十进制值。

Decimal

decimal

Decimal

Decimal

  

IntPtr

大小取决于基础平台(32 位平台上为 32 位值,64 位平台上为 64 位值)的有符号整数。

IntPtr

无内置类型。

IntPtr

无内置类型。

IntPtr

无内置类型。

IntPtr

  

UIntPtr

大小取决于基础平台的无符号整数(32 位平台上为 32 位值,64 位平台上为 64 位值)。

不符合 CLS。

UIntPtr

无内置类型。

UIntPtr

无内置类型。

UIntPtr

无内置类型。

UIntPtr

类对象

Object

对象层次结构的根。

Object

object

Object*

Object

  

String

Unicode 字符的不变的定长串。

String

string

String*

String

 

我正在做一個用C#應用程式讀取C++做成的DLL回傳的字串, 字串是用Wchar, 因為需要回傳中文字; 但是我的問題是: 我以下的公式表現非常的不穩定, 有時候有回傳字串, 但有時就沒有, 請教各位是否能指出我公式是錯在哪裡, 或能提供任何好建議, 萬分感謝。

C#:
public class FileText
{
  [DllImport(@"c:\FileText.dll", EntryPoint = "getText", SetLastError = false, CharSet = CharSet.Unicode)]
        public static extern String getText(String filename);
}

----------------------------------------------------
C++:
wchar_t * _stdcall getText(wchar_t * filename)
{
char cfilename[128];
wcharTochar(filename, cfilename, sizeof(cfilename));    
ifstream inFile(filename);

if(inFile.is_open())
{
string text, line;
text = "";
int arrLength = 0;
char * chr;
while(getline(inFile,line)) {
text.append(line, 0, line.length());
}
inFile.close();
arrLength = text.size() + 1;
chr = new char [arrLength];
strcpy(chr, text.c_str());

wchar_t *wchar = L"_";
wchar = (wchar_t *)malloc(sizeof(wchar_t) * arrLength*2-1);   
charTowchar(chr, wchar, sizeof(wchar_t) * arrLength*2-1); 
return wchar;
}else
wchar_t *emp = L" ";
return emp;
}

void charTowchar(const char *chr, wchar_t *wchar, int size)   
{      
    MultiByteToWideChar( CP_ACP, 0, chr, strlen(chr)+1, wchar, size/sizeof(wchar[0]) );   

}   

//------------------------------------------------------------------------------

void wcharTochar(const wchar_t *wchar, char *chr, int length)   
{   
    WideCharToMultiByte( CP_ACP, 0, wchar, -1, chr, length, NULL, NULL );   
}

你的 Marshal Dllimport 應該是沒什麼問題,
問題應該是出在 C++ 寫的 function......
因為你的回傳值是在 function 中宣告的 local variable,
當 function 結束後,variable 的 life cycle 也同時走入歷
史的盡頭,而被回收掉,導致 caller 去讀取時,會發生
memory violate .

解決方式:
1. 既然你的 return value 是固定的,可用 anonymous
    variable 的方式回傳,ex: return L"_";
2. 在 function 之外宣告 global variable,以 global
    variable 當作 return value (不建議;需要確保沒有
    multi-access 的 issue)
3. 由 caller 以參數的方式傳入實際的 memory space,
    callee 將要回傳的內容填入;而 return value 宣告為
    boolean,用來通知 caller 是否為有效的回傳。

直接用 MultiByteToWideChar 就好了,何必要多 wrap 一層??

[DllImport("kernel32.dll", SetLastError=true)]
static extern int MultiByteToWideChar ( int CodePage, int dwFlags,
string lpMultiByteStr, int cchMultiByte,
string lpWideCharStr, int cchWideChar)

 

 

 

抱歉!评论已关闭.