473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help calling C DLL from VB.NET

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
Nov 20 '05 #1
1 2839
>-----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, DeclareFunction, 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 eAs 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

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: VicVic | last post by:
Dear Great Experts, I have a problem and need your help! We have a Flat DLL built in C++ (CCC), and on top of it, we built another DLL built in C# (SSS), which has .Net Assembly. CCC was...
1
by: Van | last post by:
Developing in C++, .Net 2003 (7.1) Running on Windows XP SP2 My delima is that all my windows services run fine until I STOP the service at which time I get a "The windows service terminated...
0
by: RamaKrishna | last post by:
Hi All: I created a .NET web service in .NET Beta2.0...And am calling this web service from VisualBasic 6.0...using SOAP ToolKit (Both versions 2 and 3 r working fine)...It's working fine...Now...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: smerf | last post by:
I am really new to this .Net stuff, but I am wondering if you can help me with a webservice issue. I have 15 locations that need to access a webservice. The webservice accepts XML and returns...
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
5
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.