Tag: dllimport

在C ++和C#ByRef之间传递字节数组会引发AccessViolationException

我试图创建一个Win32 DLL公开一些在C#中调用的函数,如下所示 __declspec(dllexport) int GetData(unsigned char* *data, int* size) { try { int tlen = 3; unsigned char* tchr = new unsigned char[5]; tchr[0] = ‘a’; tchr[1] = ‘b’; tchr[2] = ‘c’; *size = tlen; *data = tchr; return 1; } catch (char *p) { return 0; } } 在C#方面 [DllImport(“MyDll.dll”)] static extern int GetData(ref […]

如何将函数从C ++ DLL正确导入到C#Project中

我可以使用如何正确地将C ++ DLL中的几个函数导入到我的C#应用​​程序中的帮助。 以下是C ++方面的一些示例,它们展示了我在C#中尝试做的事情。 我不认为我正确地编组了一些/或一些返回类型和一些参数(尤其是指针/ ref / out)。 C ++头文件声明: unsigned long __stdcall mfcsez_initialisation(unsigned short serial); unsigned char __stdcall mfcs_get_serial(unsigned long int handle, unsigned short * serial); unsigned char __stdcall mfcs_read_chan(unsigned long int handle, unsigned char canal, float * pressure, unsigned short * chrono); C ++代码: /* Define functions prototype */ typedef unsigned long(__stdcall […]

C#P \调用DLL没有进入C ++的入口点?

我有一个C ++ Dll“TheFoo.dll”,方法“Foo()” 我只需调用以下命令即可访问使用此方法的其他C ++代码: Foo(); 我相信该方法确实有: __declspec( dllexport ) 所以,随着我对P / Invoke所做的阅读,我认为我应该能够简单地从我的C#代码中调用相同的方法: namespace PInvokeExample1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } [DllImport(@”C:\MyFolder\TheFoo.dll”)] public static extern void Foo(); private void button1_Click(object sender, RoutedEventArgs e) { Foo(); } } } 当我运行它时,我收到一个错误: Unable to […]

调用DLL方法时,方法的类型签名不兼容PInvoke

我有一个带接口的DLL struct modeegPackage { uint8_t version; // = 2 uint8_t count; // packet counter. Increases by 1 each packet uint16_t data[6]; // 10-bit sample (= 0 – 1023) in big endian (Motorola) format uint8_t switches; // State of PD5 to PD2, in bits 3 to 0 }; __declspec(dllexport) void __cdecl initSerial(); __declspec(dllexport) void __cdecl closeSerialPort(); […]

查找屏幕键盘的类名?

我试图使用此代码示例从C#(。NET 3.5)Winforms应用程序控制Windows XP屏幕键盘(OSK.exe): [DllImport(“User32.dll”)]public static extern Int32 SetForegroundWindow(int hWnd); [DllImport(“user32.dll”)]public static extern int FindWindow(string lpClassName, string lpWindowName); private void BringToFront(string className,string CaptionName) { SetForegroundWindow(FindWindow(className,CaptionName)); } private void Form1_Load(object sender, EventArgs e) { BringToFront(“Notepad”, “Untitled – Notepad”); } 如何确定准确的className? 我假设CaptionName是’屏幕键盘’。

从C ++ DLL(使用“Dllimport”从C#加载)调用C#方法/函数

在单个标题中恢复它有点困难,所以在这里我的情况。 我正在构建一个加载C ++库的C#应用​​程序。 我从该C ++ DLL调用函数。 但我也喜欢我的C ++ DLL从C#应用程序调用函数(即导入/运行它)… 这里有一段代码使它更全面: // I’m importing functions from my native code [DllImport(dllFilePath, CallingConvention = CallingConvention.Cdecl)] public static extern int returnIntValue(int value); [DllImport(dllFilePath, CallingConvention = CallingConvention.Cdecl)] public static extern int returnIntArrValueAt(int[] values, int index); // Here is the kind of code it should be // [DllImport(dllFilePath, CallingConvention = CallingConvention.Cdecl)] […]

在P / Invoke调用上固定char

我有char缓冲区的对象池,并在P / Invoke调用上传递此缓冲区。 我是否需要在通话前固定缓冲区? 第一种方法: [DllImport(“Name”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static extern void SomeMeth(char[] text, int size); public static string CallSomeMeth() { char[] buffer = CharBufferPool.Allocate(); SomeMeth(buffer, 4095); string result = new string(buffer, 0, Array.IndexOf(buffer, ‘\0’)); CharBufferPool.Free(buffer); return result; } 第二种方法: [DllImport(“Name”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static unsafe extern […]

DllImport Unmanaged,非.NET Dll到.NET项目代表Char *和Void __StdCall

我有一个非.net的DLL和非托管的Borland C ++,我需要导入。 它返回void并在函数上具有标识符__stdcall。 它还需要传递char *。 当我尝试将其添加为VS 2005中的项目的引用时,它返回无效程序集的错误。 我怎么能在C#中做到这一点? 这是我目前拥有的,它不起作用: [DllImport (“Project1.dll”, CallingConvention=CallingConvention.StdCall)] public static extern IntPtr CustomerForm (String caption);

DllImport vs LoadLibrary,最好的方法是什么?

我通常在c#.NET中使用Win32 API。 但不要在一个应用程序中声明所有内容。 有时通常使用user32,有时gdi32 ……我想当我声明所有api函数时,那些使用大量内存。 在.NET中使用API​​的最佳方法是什么?

在C#中使用SetFilePointer已经取消了堆栈

好的,我在C#和.NET 4.0中使用SetFilePointer函数。 以下是我用来调用此函数的dllimports: [DllImport(“Kernel32.dll”, SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] static extern uint SetFilePointer([In] SafeFileHandle hFile, [In] long lDistanceToMove, [Out] out int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod); 每当我运行在调试器中使用SetFilePointer函数的代码时,我都会遇到以下exception: PInvokeStackImbalance was detected Message: A call to PInvoke function ‘MyDiskStuff!MyDiskStuff.HardDisk::SetFilePointer’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match […]