简体   繁体   中英

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.

Here is the function in the c++ dll that i want to call from my VB.net code.

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."

I have tried passing in byte(), Marshalling with LPStr, SafeArray and nothing works.

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. Try this from within a Module (not a class):

<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#. So I am writing in C# way: It might help you:

The C# End:

//Include the header file using 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); //Bascially its a function declaration

//Now finally call that function in usual way

The C++ End:

Dont forget to expose your dll function, by writing something like "__declspec(dllimport)" before your function __declspec(dllimport) bool LoadNewTestPlan(const char* szPlanFileName=" ");

If it seems to be useful to you then feel free to ask for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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