简体   繁体   English

VB.net中的dllimport C ++ DLL

[英]dllimport C++ DLL In VB.net

I'm stuck at dll imports with the c++ dll and i really need help to get over this. 我被C ++ dll限制在dll导入中,我确实需要帮助来克服这一问题。

Here is the function in the c++ dll that i want to call from my VB.net code. 这是我想从我的VB.net代码中调用的c ++ dll中的函数。

bool LoadNewTestPlan(const char* szPlanFileName=" "); bool LoadNewTestPlan(const char * szPlanFileName =“”);

I've tried many ways in my VB.net but always getting the error : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." 我在VB.net中尝试了许多方法,但始终收到错误消息:“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”

I have tried passing in byte(), Marshalling with LPStr, SafeArray and nothing works. 我尝试传递byte(),使用LPStr,SafeArray进行编组,但没有任何效果。

Here is the example of my code code within the module 这是我在模块中的代码示例

<DllImport("HPVKIfc.dll", EntryPoint:="?LoadNewTestPlan@HPVKIfc@@QAE_NPBD@Z", CharSet:=CharSet.Ansi)> _
Public Function LoadNewTestPlan(<MarshalAs(UnmanagedType.LPStr)> ByVal pln As String) As Boolean

End Function

Do you see anything wrong? 你有什么不对吗?

Thanks in advance. 提前致谢。

Unless you have specified otherwise, a free function (not a member of a class) will probably use the C calling convention. 除非您另外指定,否则自由函数(不是类的成员)可能会使用C调用约定。 Try this from within a Module (not a class): Module (不是类)中尝试以下操作:

<DllImport("MyLib.dll", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Ansi)> _
Private Function LoadNewTestPlan(PlanFilename As String) As Boolean
End Function

I never use VB.net, but achieve the same functionality in C#. 我从不使用VB.net,但在C#中实现了相同的功能。 So I am writing in C# way: It might help you: 所以我用C#方式编写:它可能对您有帮助:

The C# End: C#结束:

//Include the header file using System.Runtime.InteropServices; //使用System.Runtime.InteropServices包含头文件; //Then write the follwing two line any wehre in global scope [DllImport("NameOfYourDLL.dll")] private static extern void NameOfYourFunction(//Function parameters if any); //然后在全局范围内的任何位置写以下两行[DllImport(“ NameOfYourDLL.dll”)] private static extern void NameOfYourFunction(//函数参数(如果有)); //Bascially its a function declaration //特别是它的一个函数声明

//Now finally call that function in usual way //现在终于以通常的方式调用该函数

The C++ End: C ++结束:

Dont forget to expose your dll function, by writing something like "__declspec(dllimport)" before your function __declspec(dllimport) bool LoadNewTestPlan(const char* szPlanFileName=" "); 不要忘记通过在函数__declspec(dllimport)之前编写“ __declspec(dllimport)”之类的东西来公开dll函数。bool LoadNewTestPlan(const char * szPlanFileName =“”);

If it seems to be useful to you then feel free to ask for further details. 如果这对您似乎有用,请随时询问更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM