Answered by:
C++ DLL in VB project

Question
-
Hello,
I have been building a C++ DLL for a visual basic project. The VB finds the DLL and allow me to step through it while debugging. However, the DLL only passes in the first parameter of the subroutine call. All other parameters show an invalid memory location error. The VB looks something like this:
Imports System.Runtime.InteropServices Public Class Form1 <DllImport("interpolation.dll", ExactSpelling:=False, CallingConvention:=CallingConvention.Cdecl)> Shared Function main(ByVal xin As Single, ByVal yin As Single) As Single End Function Private Sub Form1_Load(sender As Object, e As Dim a As Single Dim b As Single a = 26000 b = 20 Label2.Text = main(a, b) End Sub
and the C++ like this:
#include <iostream> #include <stdlib.h> #include <string> #define DllImport __declspec(dllimport) #define DllExport extern "C" __declspec(dllexport) using namespace std; DllExport float main(DllImport float xin, DllImport float yin) { }
I have omitted the body of the code but left all of the call. I believe I have most of the syntax correct but can not figure out why it only passes in xin but not yin. Like I said before it shows an invalid memory location. xin come in correct as a float, I don't know why yin does not. I have been working on this a few month and have tried many different combinations. Any advice would be very appreciated.
Thank you!
- Moved by lake Xiao Monday, March 14, 2016 7:13 AM
Sunday, March 13, 2016 2:54 AM
Answers
-
I think that DllImport is not required in DLL and can be removed. Make sure that your program uses the right and last DLL file and you are debugging the Debug configuration. For investigations, you can even specify the full absolute path in the <DllImport> attribute.
Is it just a problem of debugger? Does it return a correct value in this simplified case:
DllExport float __cdecl main( float xin, float yin )
{
return xin + yin;
}
- Edited by Viorel_MVP Monday, March 14, 2016 5:40 AM Spelling.
- Proposed as answer by lake Xiao Monday, March 14, 2016 6:17 AM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, March 21, 2016 3:34 AM
Sunday, March 13, 2016 8:16 AM
All replies
-
I think that DllImport is not required in DLL and can be removed. Make sure that your program uses the right and last DLL file and you are debugging the Debug configuration. For investigations, you can even specify the full absolute path in the <DllImport> attribute.
Is it just a problem of debugger? Does it return a correct value in this simplified case:
DllExport float __cdecl main( float xin, float yin )
{
return xin + yin;
}
- Edited by Viorel_MVP Monday, March 14, 2016 5:40 AM Spelling.
- Proposed as answer by lake Xiao Monday, March 14, 2016 6:17 AM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, March 21, 2016 3:34 AM
Sunday, March 13, 2016 8:16 AM -
Wow I think you might be right. I was running a pretty complex C++ routine so it was hard to see if it was calculating the value correctly. Yea the DllImport was something I was just messing around with. It seems to have no effect on the subroutine output. The Cdecl call vs. Stdcall doesn't seem to make a difference either. Im not sure which one is best but ill stick with Cdecl as it seems to be the popular opinion on Stacked. Thank for you help mate, I wasn't sure if I was going crazy or not.Sunday, March 13, 2016 8:37 PM
-
Hi Awilhelm99,
Since your issue is about VB.NET. I moved it to the VB forum for better support.
Thanks for your understanding.
Best Regards,
Lake Xiao
Monday, March 14, 2016 7:12 AM