vbnoob2503
New member
- Joined
- Feb 10, 2009
- Messages
- 3
Hi folks,
I try to use a DLL file (ReadIMX.DLL) generated with C++ in my VB application. I have the DLL sourcecode and I guess I know how to call the DLL from VB. Still I have some trouble with generating structures like in the C++ code I need calling the DLL.
The DLL basically has the decryption routine to read special image files (*.IMG, *.IMX, ...) associated with a laboratory camera. Looking at the C++ code the DLL is called as follows:
Giving the filename to the DLL it decrypts the image file and stores data into myBuffer. The myList list includes image attributes. Setting myList = NULL, the DLL does not read those attributes. As I am not interested in the attributes I will use the NULL argument here. The DLL returns an integer representing an error code. The BufferType structure is defined like this:
I tried to define the structures in my VB script as follows:
Calling the DLL as follows I get an error message pasted below:
The error message says (in my VB version the message is German, so please excuse if my translation is not conform with VB error expressions):
I try to use a DLL file (ReadIMX.DLL) generated with C++ in my VB application. I have the DLL sourcecode and I guess I know how to call the DLL from VB. Still I have some trouble with generating structures like in the C++ code I need calling the DLL.
The DLL basically has the decryption routine to read special image files (*.IMG, *.IMX, ...) associated with a laboratory camera. Looking at the C++ code the DLL is called as follows:
Code:
extern "C" int EXPORT ReadIMX ( const char* theFileName, BufferType* myBuffer, AttributeList** myList )
Code:
typedef struct
{
float factor;
float offset;
char description[16];
char unit[16];
} BufferScaleType;
typedef struct
{
int isFloat;
int nx,ny,nz,nf;
int totalLines;
int vectorGrid; // 0 for images
int image_sub_type; // BufferFormat_t
union
{
float* floatArray;
Word* wordArray;
};
BufferScaleType scaleX; // x-scale
BufferScaleType scaleY; // y-scale
BufferScaleType scaleI; // intensity scale
} BufferType;
Code:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential)> _
Public Structure BufferScaleType
Public factor As Single
Public offset As Single
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _
Public description As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> _
Public unit As String
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure Union
<FieldOffset(0)> _
Public float As Single()
<FieldOffset(0)> _
Public Word As String()
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure BufferType
Public isFloat As Integer
Public nx, ny, nz, nf As Integer
Public totalLines As Integer
Public vectorGrid As Integer
Public image_sub_type As Integer
Public Unionx As Union
Public scalex As BufferScaleType
Public scaley As BufferScaleType
Public scalei As BufferScaleType
End Structure
Code:
Public Declare Function ReadIMX Lib "ReadIMX.dll" (ByVal filename As String, ByRef btype As BufferType, ByRef attributelist As Object) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filename As String = "D:\Daten\Messungen\Test\429mbarAr_7_5kHz_740Vpp_696_64nm\B0001.IMG"
Dim buftype As New BufferType
Dim A As Integer = ReadIMX(filename, buftype, System.DBNull.Value)
End Sub
Do you have an idea where the problem is hiding? I attached the DLL and a sample IMG-file to this post.InvalidVariant was detected.
Converting a none managed VARIANT to a managed object an invalid INVARIANT was found. If an invalid INVARIANT is passed to the CLR, unexpected exceptions, data harm or loss can occur.