How to initialize a structure in VB and how to create an instance.

Hi.

I have 2 programs which are: a DLL in C and a VB application that uses DLL functions.

Flow: The VB application calls the functions in the C DLL and also VB aplication passes some arguments to the functions of the C DLL.

My Query:

I have a structure whose pointer has to be passed as an argument to the DLL function.How can we initialize a structure in VB.

Is there any way to pass the instance of the structure(for example in C we pass the pointer to the function to access all the members of the structure)

Eagerly waiting for the reply.

Thanks in Advance

[615 byte] By [satya999] at [2007-12-23]
# 1

I'm assuming you just want to pass the same instance of the structure you're working on... I reccomend 2 things, either passing it as an Argument using ByRef


Public Sub Test(ByRef instance)

If you were using a class you could use something called a Singleton Pattern. This involves creating an instance of the class withtin itself and hiding the default constructor this way you create a static method that returns that internal instance meaning you can have 6 classes using the same instance of a class.



Public NotInheritable Class Singleton
' Create an internal shared instance
Private Shared _singleton As Singleton = New Singleton()
' Hide the default constructor
Private Sub New()
End Sub
' Method to extract the internal shared instance
Public Shared Function GetInstance() As Singleton
Return _singleton
End Function
End Class

Hope this helps

-Chad

ChadMoran at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

If the structure is being passed to the C DLL, then it's actually just passing a pointer to the structure. There are several problems with doing this:

VB can change the location of the structure - in which case you are going to get serious errors and crashes with your program.

The structure needs to be of the EXACT structure - including byte boundaries - that the C DLL is expecting.

You need to marshal the structure correctly to the function. This means you need to arrange the structure correctly (without knowing what it is, this'd be hard to tell you how to do it). You then need to assign some memory and copy the structure to it (if you are passing data to the function through the structure). Then you pass a pointer, instead of the structure, to the DLL. The function does its stuff, then you can marshal the memory back to your structure.

Since you have only talked in the abstract, the above is also fairly abstract. But you can look up the namespace System.Runtime.InteropServices and look at the Marshal class.

SJWhiteley at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Hi.

I am currently working on VB 6.0 which doesnt consist of all the functions tha you have mentioned in your reply.

satya999 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...