01-07-2013 04:25 AM - edited 01-07-2013 04:31 AM
I am trying to Call Function of a simple DLL written in CVI9.0 from VB.Net 2008 (Windows 7, .Net 3.5, 64-Bit )
Using Below Code, I am getting an Error:: "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"
Can any one Help me, I am Stuck, What am I Missing ! Unable to Get any Previous Example on "Calling CVI Dll in VB.net",
Here's my Present Code
CVI:
int __declspec(dllexport) __cdecl GetAnalogVoltage(int Channel)
{
int x = Channel + 5;
return x;
}
extern int __declspec(dllexport) __cdecl GetAnalogVoltage(int Channel);
The CVI Exports GetAnalogVoltage Function to my DLL, Which I can access from Test Stand . etc... But How Can I call this function from VB.NET ?
VB.NET:
Public Class Form1
Inherits System.Windows.Forms.Form
<DllImport("C:\CLTS.dll")> _
Public Shared Function GetAnalogVoltage(ByVal Channel As Integer) As Integer
End Function
Private Sub CmdGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdGet.Click
Dim Result AsInteger
Result = GetAnalogVoltage(1)
End Sub
End Class
01-21-2013 02:03 PM - edited 01-21-2013 02:08 PM
I don't know if you solved this on your own already, but figured I'd post the solution either way.
The problem is that the default calling convention for DllImport in VB .NET is WinAPI. Since you are using __cdecl, you will need to pass this setting as a config parameter as shown below:
<DllImport("C:\CLTS.dll", CallingConvention:=CallingConvention.Cdecl)> _ Public Shared Function GetAnalogVoltage(ByVal Channel As Integer) As Integer End Function
Edit/PS: I reread your post and realized that the error you were getting was "... load a program with an incorrect format ...". This may be related to 32-bit / 64-bit compatibility. Your VB .NET application may be configured to Any CPU for the Target Platform by default. This means that on a 32-bit OS the app will run as a 32-bit app, and on 64-bit it will run as a 64-bit app. But if your DLL from CVI is a 32-bit DLL, it will not be able to load by your app on a 64-bit OS because you app is 64-bit. Instead, I would recommend choosing either 32-bit or 64-bit for the Target Platform of your VB .NET project. To do this, go to Project Properties >> Compile tab >> Advanced Compile Options, then change the Target Platform.
01-21-2013 08:40 PM
You are Right the issue is with the 32-bit / 64-bit compatibility. and was able to fix this with the Advanced Compile Options -> Target Platform. Thanks for the Comment.
The Issue is with the Platform Selection. Spent more than an hour to find that out.
Aside, These Integration issues were never highlighted in any of the Site. I would Suggest that NI CVI Error dialog can show some Possible reason for the failure in the Error Itself as it is done in .NET and other Programming Languages. Would save heck a lot of time for developers, Always we are pushed to goto forum's.
Thanks for the Comment.