Tag: dllimport

从本机dll生成C#DLLImport声明

你知道一个软件从一个本机DLL自动生成C#代码(在.cs中有[DllImport]属性),以便在C#代码中使用这个DLL吗?

如何将const char * API导入C#?

鉴于此C API声明如何将其导入C#? const char* _stdcall z4LLkGetKeySTD(void); 我已经能够做到这一点: [DllImport(“zip4_w32.dll”, CallingConvention = CallingConvention.StdCall, EntryPoint = “z4LLkGetKeySTD”, ExactSpelling = false)] private extern static const char* z4LLkGetKeySTD();

参数化DllImport用于C#应用程序

我们有一个供应商,提供访问其硬件的库。 不幸的是,如果您有多个设备,则需要多次导入其库,并使用不同的dll名称。 因此,我们有一吨重复的代码,我担心它很快就会成为维护的噩梦。 我们现在所拥有的是: namespace MyNamespace { public static class Device01 { public const string DLL_NAME = @”Device01.dll”; [DllImport(DLL_NAME, EntryPoint = “_function1”)] public static extern int Function1(byte[] param); … [DllImport(DLL_NAME, EntryPoint = “_function99″)] public static extern int Function99(int param); } …. public static class Device16 { public const string DLL_NAME = @”Device16.dll”; [DllImport(DLL_NAME, EntryPoint = […]

如何设置C ++函数以便p / invoke可以使用它?

希望这是一个无脑的简单问题,但它表明我缺乏C ++的专业知识。 我是一名C#程序员,过去我和其他人的C ++ / C dll一起完成了P / Invoke的大量工作。 但是,这次我决定自己编写一个包装器C ++ DLL(非托管),然后从C#调用我的包装器dll。 我遇到的问题是我无法定义可以通过p / invoke找到的C ++函数。 我不知道这是什么语法,但这是我到目前为止所做的: extern bool __cdecl TestFunc() { return true; } 最初我只是有这个,但它也没有用: bool TestFunc() { return true; } 然后在C#方面,我有: public const string InterfaceLibrary = @”Plugins\TestDLL.dll”; [DllImport( InterfaceLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = “TestFunc” ), SuppressUnmanagedCodeSecurity] internal static extern bool TestFunc(); 所有东西都编译,但是当我执行这个C#p […]

在c#中以编程方式设置dllimport

我在我的解决方案中使用DllImport 。 我的问题是我有两个版本的相同的DLL一个为32位而另一个为64位。 它们都使用相同的名称和相同的签名来公开相同的function。 我的问题是我必须使用两个静态方法来暴露这些,然后在运行时使用IntPtr大小来确定要调用的正确方法。 private static class Ccf_32 { [DllImport(myDllName32)] public static extern int func1(); } private static class Ccf_64 { [DllImport(myDllName64)] public static extern int func1(); } 我必须这样做,因为myDllName32和myDllName64必须是常量,我没有找到在运行时设置它的方法。 有没有人有一个优雅的解决方案,所以我可以摆脱代码重复和常量IntPtr大小检查。 如果我可以设置文件名,我只需要检查一次,我就可以摆脱大量重复的代码。

无法阅读其他应用程序的标题

跳跃我将如何在我的主程序中找到窗口句柄… 在C#中 我运行notepad.exe然后在其中键入内容,然后使用SPY ++(0x111111)找到主窗口句柄,并且 [DllImport(“user32.dll”, SetLastError = true, CharSet = CharSet.Auto)] internal static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount); . . . GetWindowText((IntPtr)(0x111111), str, 1024); 这段代码工作正常,并返回主窗口的标题。 ::但是当我做同样的事情来找到notepad.exe的孩子的标题时,它只是将str设置为空。 间谍++告诉我孩子的标题有价值。

在C ++中将它们从C#发送到OpenCV后得到扭曲的图像?

我从我的C ++类创建了一个C DLL,它使用OpenCV进行图像处理,并希望在我的C#应用​​程序中使用这个DLL。 目前,这就是我实现它的方式: #ifdef CDLL2_EXPORTS #define CDLL2_API __declspec(dllexport) #else #define CDLL2_API __declspec(dllimport) #endif #include “../classification.h” extern “C” { CDLL2_API void Classify_image(unsigned char* img_pointer, unsigned int height, unsigned int width, char* out_result, int* length_of_out_result, int top_n_results = 2); //… } C#相关代码: DLL导入部分: //Dll import [DllImport(@”CDll2.dll”, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] static extern void Classify_Image(IntPtr […]

DllImport返回null终止字符串AccessViolationException

我试图与实现几个函数的Dll接口,其中一个函数接受一个空终止字符串和一个int,并返回一个以空字符结尾的字符串。 我试图与这样的方法接口: [DllImport(dll_loc)] [return : MarshalAs(UnmanagedType.LPStr)] public static extern StringBuilder GetErrorMessage([MarshalAs(UnmanagedType.LPStr)] StringBuilder message, int error_code); 然后我尝试像这样调用方法: StringBuilder message = new StringBuilder(1000); StringBuilder out2 = new StringBuilder(1000); out2 = GetErrorMessage(message, res0); 但是,当我尝试这个时,抛出一个AccessViolationException告诉我我正在尝试访问受保护的内存。 我成功地宣布了一个不同的方法: [DllImport(dll_loc)] public static extern int GetVersion([MarshalAs(UnmanagedType.LPStr)] StringBuilder version); 并以相同的方式调用它,但此方法不适用于当前函数调用。 我也尝试返回一个IntPtr,因为文档在技术上说该方法返回一个指向字符串缓冲区的第一个字符的指针,但无济于事。 有没有人对这里可能出现的问题有任何见解? 导致dll尝试访问内存的这两种方法之间可能有什么不同。 或者,您如何建议调试此问题?

SQLite更新后:无法在DLL“SQLite.Interop.dll”中找到名为“sqlite3_changes_interop”的入口点

我的C#/ SQLite工作正常,直到我决定更新SQLite DLL(从1.0.82.0到1.0.84.0)。 现在我遇到了这个崩溃: 无法在DLL“SQLite.Interop.dll”中找到名为“sqlite3_changes_interop”的入口点 A first chance exception of type ‘System.EntryPointNotFoundException’ occurred in System.Data.SQLite.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionSparkleShare.vshost.exeSystem.EntryPointNotFoundException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Unable to find an entry point named ‘sqlite3_changes_interop’ in DLL ‘SQLite.Interop.dll’. at System.Data.SQLite.UnsafeNativeMethods.sqlite3_changes_interop(IntPtr db) at System.Data.SQLite.SQLite3.get_Changes() at System.Data.SQLite.SQLiteStatement.TryGetChanges(Int32& changes) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() […]

如何在C#中使用RegisterHotKey()?

我正在尝试注册一个热键,我正在翻译这个 C ++代码,我写道: using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class Program { [DllImport(“user32.dll”)] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk); [DllImport(“user32”)] public static extern bool GetMessage(ref Message lpMsg, IntPtr handle, uint mMsgFilterInMain, uint mMsgFilterMax); public const int MOD_ALT = 0x0001; public const int MOD_CONTROL = […]