草庐IT

C# 应用程序调用 C++ 方法,错误 : PInvoke: Cannot return variants

我正在尝试弄清楚如何将复杂对象从C++dll返回到调用C#应用程序。我有一个简单的方法,它返回一个工作正常的int。谁能告诉我我做错了什么?C#应用程序:classProgram{staticvoidMain(string[]args){//Erroronthisline:"PInvoke:Cannotreturnvariants"vartoken=LexerInterop.next_token();}}C#LexerInterop代码:publicclassLexerInterop{[DllImport("Lexer.dll")]publicstaticexternobjectnex

从 Windows 而不是从 Visual Studio 运行时,C#/C++ 应用程序崩溃

我正在开发调用非托管(C++)dll的C#应用程序。我发现某个用户操作在从Windows资源管理器运行时始终会导致应用程序崩溃。但是,从VisualStudio调试器启动时,不会发生崩溃。因此,我无法在崩溃发生时进入代码并准确调试发生了什么。从Explorer而不是从VisualStudio运行时,什么可能导致二进制文件崩溃?请注意,我使用的是发布版本;调试构建在VisualStudio和Explorer中都不会崩溃。(如果相关,我可以说崩溃与在C++DLL中操作malloc分配的数组有关。我通过煞费苦心地注释掉代码块、重建、从Windows运行并检查是否或没有发生崩溃。但是,我已经达

c# - 我的 C++ 和 C# 互操作在 64 位中崩溃,为什么?指针大小?

我有一个native32位dll(无源代码),它在我使用的应用程序中作为插件运行。我自己做了另一个nativedll,它将与该插件通信以创建和更新插件的控件。我从那个dll导出了我需要的功能,以便从我的c#应用程序(使用p/invoke)控制插件。代码如下:h文件:#pragmaonce#include"include\SpoutControls.h"extern"C"{__declspec(dllexport)voidInitializeControls(char*sendername,int*numControls,char**names,int*types,float*float

c# - 帮我把 C++ 结构转换成 C#

我是C#的新手,需要帮助将C++结构转换为C#。C++结构如下:#defineQUE_ADDR_BUF_LENGTH50#defineQUE_POST_BUF_LENGTH11typedefstruct{constWCHAR*streetAddress;constWCHAR*city;constWCHAR*state;constWCHAR*country;constWCHAR*postalCode;}QueSelectAddressType;typedefstruct{WCHARstreetAddress[QUE_ADDR_BUF_LENGTH+1];WCHARcity[QUE_ADD

c# - 如何更改我的 C++ 代码以用作 C# 中的 DLL?

我正在学习如何从C#调用C++中的方法。我做了一些研究,Pinvoke似乎是一种不错的方式。如何将这个简单的C++代码转换成它应该如何在C#中调用的方式,以及如何编写要在C#中调用的方法?我有一个头文件:数学函数库.hnamespaceMathFuncs{classMyMathFuncs{public:doubleAdd(doublea,doubleb);MyMathFuncsgetClass();};}数学函数库.cpp#include"MathFuncsLib.h"namespaceMathFuncs{MyMathFuncsMyMathFuncs::getClass(){retur

c# - 如何通过禁用 Name Mangling 来调用实例方法

给定foo.dll中的以下c++类classa{private:int_answer;public:a(intanswer){_answer=answer;}__declspec(dllexport)intGetAnswer(){return_answer;}}我想要来自C#的pInvokeGetAnswer。为此,我使用以下方法:[DllImport("foo.dll",CallingConvention=CallingConvention.ThisCall,EntryPoint="something")]publicstaticexternintGetAnswer(IntPtrth

c# - PInvoke:在 C++ 中分配内存并在 C# 中释放它

我们正在使用PInvoke在C#和C++之间进行互操作。我有一个如下所示的互操作结构,另一侧具有相同布局的C++结构。[StructLayout(LayoutKind.Sequential)]publicstructMeshDataStruct:IDisposable{publicMeshDataStruct(double[]vertices,int[]triangles,int[]surfaces){_vertex_count=vertices.Length/3;_vertices=Marshal.AllocHGlobal(_vertex_count*3*sizeof(double)

c# - 托管 .NET 等同于 WinBase 的 CreateFile 和 WriteFile (kernel32.dll)

我正在使用旧文件格式。该文件是使用使用WinBase.hCreateFile()和WriteFile()函数(在kernel32.dll中找到)的非托管C++创建的。我一直在使用P/Invoke互操作来访问这些native函数,如下所示:[DllImport("kernel32.dll")]publicstaticexternboolWriteFile(IntPtrhFile,byte[]lpBuffer,uintnNumberOfBytesToWrite,outuintlpNumberOfBytesWritten,[In]refNativeOverlappedlpOverlapped

c# - __declspec(dllexport)::vector <std::string>

我一直在努力弄清楚如何将字符串数组从C++DLL返回到C#应用程序,但我对如何执行此操作或在非常基础的级别上查找文章感到困惑。假设我有下面的代码。如何修复粗体线:extern"C"{__declspec(dllexport)intGetANumber();//unsureonthisline:**__declspec(dllexport)::vectorListDevices();**}extern::vectorGetStrings(){vectorseqs;returnseqs;}externintGetANumber(){return27;}谢谢马特

c# - 如何在托管代码 (C#) 中从 native 代码 (C++) 获取字符串数组

有什么办法可以从c++到c#获取字符串集合C#代码[DllImport("MyDLL.dll")]privatestaticexternListGetCollection();publicstaticListReturnCollection(){returnGetCollection();}C++代码std::vectorGetCollection(){std::vectorcollect;returncollect;}以上代码仅为示例,主要目的是从C++中获取C#中的集合,不胜感激//詹姆士 最佳答案 有多种方法可以解决这个问题,