我匯入了一個用 rato.hpp 編譯的 DLL:
#ifndef RATO_H
#define RATO_H
extern "C"
{
__declspec(dllexport) void mouse_move(char button, char x, char y, char wheel);
}
#endif
和rato.cpp:
typedef struct {
char button;
char x;
char y;
char wheel;
char unk1;
} MOUSE_IO;
void mouse_move(char button, char x, char y, char wheel)
{
MOUSE_IO io;
io.unk1 = 0;
io.button = button;
io.x = x;
io.y = y;
io.wheel = wheel;
if (!callmouse(&io)) {
mouse_close();
mouse_open();
}
}
在我的 CSharp 代碼中匯入它時,我不能轉換負值:
[DllImport("Rato.dll")]
public static extern void mouse_move(char button, char x, char y, char wheel);
mouse_move((char)0,(char)0,(char)-150,0);
我嘗試轉換,但它不起作用,它不斷發送正值而不是負值。
編輯:嘗試轉換為 Sbyte 但收到錯誤“System.OverflowException:'對于有符號位元組而言,值太大或太小。'”
[DllImport("Rato.dll")]
public static extern void mouse_move(sbyte button, sbyte x, sbyte y, sbyte wheel);
mouse_move(0,0,Convert.ToSByte(-150),0);
我怎樣才能成功地將負字符值傳遞給 C 匯入的 Dll?
在此先感謝并為我的英語不好感到抱歉。
uj5u.com熱心網友回復:
char在 C# 中是 16 位無符號型別,因此顯然您不能使用它與charC 中的互操作。sbyte是要走的路。然而,范圍sbyte是 -128 到 127,類似于unsigned charC 中的 when CHAR_BIT == 8,所以顯然 -150 不適合 C# sbyte/Cchar
注意:char在 C 中可以是有符號的或無符號的,因此,例如,如果您在專案/J中使用 MSVC中的選項或-funsigned-charGCC中的選項,那么您不能將負值傳遞給 C 函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/493395.html
上一篇:C-你可以在外部外部函式中釋放記憶體嗎?你如何驗證這一點?
下一篇:該用戶帳戶所有者的權限
