Help calling C DLL from VB.NET

  • Thread starter Thread starter Paul Ferrill
  • Start date Start date
P

Paul Ferrill

Can anyone help with the code to call the following C routines:

/* Memory model that allows to access memory at offsets of lzo_uint.
*/
#if !defined(__LZO_MMODEL)
# if (LZO_UINT_MAX <= UINT_MAX)
# define __LZO_MMODEL
# elif defined(__LZO_DOS16) || defined(__LZO_WIN16)
# define __LZO_MMODEL __huge
# define LZO_999_UNSUPPORTED
# elif defined(__LZO_PALMOS16) || defined(__LZO_TOS16)
# define __LZO_MMODEL
# else
# error "__LZO_MMODEL"
# endif
#endif

/* no typedef here because of const-pointer issues */
#define lzo_byte unsigned char __LZO_MMODEL
#define lzo_bytep unsigned char __LZO_MMODEL *
#define lzo_charp char __LZO_MMODEL *
#define lzo_voidp void __LZO_MMODEL *
#define lzo_shortp short __LZO_MMODEL *
#define lzo_ushortp unsigned short __LZO_MMODEL *
#define lzo_uint32p lzo_uint32 __LZO_MMODEL *
#define lzo_int32p lzo_int32 __LZO_MMODEL *
#define lzo_uintp lzo_uint __LZO_MMODEL *
#define lzo_intp lzo_int __LZO_MMODEL *
#define lzo_voidpp lzo_voidp __LZO_MMODEL *
#define lzo_bytepp lzo_bytep __LZO_MMODEL *

LZO_EXTERN(int)
lzo1x_decompress ( const lzo_byte *src, lzo_uint src_len,
lzo_byte *dst, lzo_uintp dst_len,
lzo_voidp wrkmem /* NOT USED */ );

LZO_EXTERN(int)
lzo1x_1_compress ( const lzo_byte *src, lzo_uint src_len,
lzo_byte *dst, lzo_uintp dst_len,

lzo_voidp wrkmem );

I've tried multiple combinations of ByVal, ByRef, DllImport, Declare
Function, etc. and all seem to throw the same exception of:

Object reference not set ton an instance of an object.

Here's the code I'm currently trying:

Public Class Class1

Declare Function lzo1x_1_compress Lib "lzo.dll" ( _
<[In](), Out()> ByVal Src() As Integer, _
ByVal SrcLen As Integer, _
<[In](), Out()> ByVal Dst() As Integer, _
ByVal DstLen As Integer, _
ByRef wrkMem As IntPtr) As Integer

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim i, r, x, y As Integer
Dim ibuff(1024), obuff(2048), wrkmem(8192) As Integer

For i = 1 To 1024
ibuff(i) = i
Next i

x = 1024

Dim mbuffer As IntPtr =
Marshal.AllocCoTaskMem(Marshal.SizeOf(y) * 8192)

Try
r = Class1.lzo1x_1_compress(ibuff, x, obuff, y, mbuffer)
Catch ex As Exception
Debug.WriteLine("Exception " & ex.Message)
End Try
Debug.WriteLine("Result " & r)
End Sub
 
-----Original Message-----
Can anyone help with the code to call the following C routines:

/* Memory model that allows to access memory at offsets of lzo_uint.
*/
#if !defined(__LZO_MMODEL)
# if (LZO_UINT_MAX <= UINT_MAX)
# define __LZO_MMODEL
# elif defined(__LZO_DOS16) || defined(__LZO_WIN16)
# define __LZO_MMODEL __huge
# define LZO_999_UNSUPPORTED
# elif defined(__LZO_PALMOS16) || defined(__LZO_TOS16)
# define __LZO_MMODEL
# else
# error "__LZO_MMODEL"
# endif
#endif

/* no typedef here because of const-pointer issues */
#define lzo_byte unsigned char __LZO_MMODEL
#define lzo_bytep unsigned char __LZO_MMODEL *
#define lzo_charp char __LZO_MMODEL *
#define lzo_voidp void __LZO_MMODEL *
#define lzo_shortp short __LZO_MMODEL *
#define lzo_ushortp unsigned short __LZO_MMODEL *
#define lzo_uint32p lzo_uint32 __LZO_MMODEL *
#define lzo_int32p lzo_int32 __LZO_MMODEL *
#define lzo_uintp lzo_uint __LZO_MMODEL *
#define lzo_intp lzo_int __LZO_MMODEL *
#define lzo_voidpp lzo_voidp __LZO_MMODEL *
#define lzo_bytepp lzo_bytep __LZO_MMODEL *

LZO_EXTERN(int)
lzo1x_decompress ( const lzo_byte *src, lzo_uint src_len,
lzo_byte *dst, lzo_uintp dst_len,
lzo_voidp wrkmem /* NOT USED */ );

LZO_EXTERN(int)
lzo1x_1_compress ( const lzo_byte *src, lzo_uint src_len,
lzo_byte *dst, lzo_uintp dst_len,
lzo_voidp wrkmem );

I've tried multiple combinations of ByVal, ByRef, DllImport, Declare
Function, etc. and all seem to throw the same exception of:

Object reference not set ton an instance of an object.

Here's the code I'm currently trying:

Public Class Class1

Declare Function lzo1x_1_compress Lib "lzo.dll" ( _
<[In](), Out()> ByVal Src() As Integer, _
ByVal SrcLen As Integer, _
<[In](), Out()> ByVal Dst() As Integer, _
ByVal DstLen As Integer, _
ByRef wrkMem As IntPtr) As Integer

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim i, r, x, y As Integer
Dim ibuff(1024), obuff(2048), wrkmem(8192) As Integer

For i = 1 To 1024
ibuff(i) = i
Next i

x = 1024

Dim mbuffer As IntPtr =
Marshal.AllocCoTaskMem(Marshal.SizeOf(y) * 8192)

Try
r = Class1.lzo1x_1_compress(ibuff, x, obuff, y, mbuffer)
Catch ex As Exception
Debug.WriteLine("Exception " & ex.Message)
End Try
Debug.WriteLine("Result " & r)
End Sub
.

This may help

** VB.NET file

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> Public Structure Foo
Public AccessStatus As Integer
Public UserLoginID As Integer
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)>
Public Capability() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)>
Public VoicePath() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public Password() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public UserName() As Byte
Public Sub New(ByVal intUnsage As Integer)
ReDim Capability(128)
ReDim VoicePath(128)
ReDim UserName(20)
ReDim Password(20)
End Sub
End Structure

Public Module TestingPassingUDT

<DllImport("c:\pat\exe\testpassingudt")> Private
Function _
TestPassing(ByRef f As Foo) As Integer
End Function

Public Sub Main()
Dim f As Foo = New Foo(0)
Dim status As Integer
Dim strData As String
Dim intPosition As Integer
f.AccessStatus = 10
f.UserLoginID = 255
Encoding.ASCII.GetBytes("Hello world".ToCharArray
(), 0, 11, f.UserName, 0)
Encoding.ASCII.GetBytes("My Password".ToCharArray
(), 0, 11, f.Password, 0)
f.UserName(15) = 0
status = TestPassing(f)
Console.WriteLine("CS {0:X}", status)
strData = Encoding.ASCII.GetString(f.UserName)
intPosition = strData.IndexOf(Chr(0))
strData = strData.Substring(0, intPosition)
Console.WriteLine("UN ={0}=", strData)
Console.WriteLine("UN <{0}>",
Encoding.ASCII.GetString(f.UserName))
Console.WriteLine("PW <{0}>",
Encoding.ASCII.GetString(f.Password))
Console.WriteLine("VP <{0}>",
Encoding.ASCII.GetString(f.VoicePath))
Console.WriteLine("CP <{0}>",
Encoding.ASCII.GetString(f.Capability))
Console.WriteLine("AS {0:X}", f.AccessStatus)
Console.WriteLine("UL {0:X}", f.UserLoginID)
End Sub

End Module

** C++ file

#include <windows.h>
#include <stdio.h>

typedef struct
{
int AccessStatus;
int UserLoginID;
BYTE abytCapability [ 128 ];
BYTE abytVoicePath [ 128 ];
BYTE abytPassword [ 20 ];
BYTE abytUserName [ 20 ];
} TypeTestPassingUDT;

extern "C" __declspec(dllexport) DWORD __stdcall
TestPassing

(TypeTestPassingUDT * pUserData)

{
char szTemp [ 128 ];

memcpy (szTemp,
(char *) pUserData->abytUserName,
16);
szTemp [ 16 ] = 0;
printf ("DLL UserName <%s>\n",szTemp);

memcpy (szTemp,
(char *) pUserData->abytPassword,
16);
szTemp [ 16 ] = 0;
printf ("DLL Password <%s>\n",szTemp);

sprintf ((char *) pUserData->abytUserName,
"{HI-%s}",
szTemp);
strcpy ((char *) pUserData->abytVoicePath,
"J:VoicePath");
strcpy ((char *) pUserData->abytCapability,
"This is the capability");
pUserData->AccessStatus = 0x12345678;
pUserData->UserLoginID = 0xFEDCBA98;
return 0xAABBCCDD;
} // TestPassing ()

** def file for building dll

LIBRARY TestPassingUDT
EXPORTS
TestPassing @1
 
Back
Top