Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual basic 6 - linking to a dll file ?

Status
Not open for further replies.

chennaiprogrammer

Programmer
Jul 31, 1999
54
I am wanting to integrate a custom dll into my Visual Basic 6

and the steps given are

(1)Add MRTLib.h and MRTLib.dll to a project.

(2)Implement LoadLibrary() Win32 API function to load “MRTLib.dll”

(3)Following (2), get function pointer address for each function of the DLL with GetProcAddress() API.

(3) Implement your program code.

(4) Build and debug.

Im stuck at the first step

Im not able to add project->reference to MRTLib.dll since visual studio says "can't add a reference to the specific file"

But if I add these lines the visual basic project compiled

Private Declare Function MRT_Init _
Lib "C:\MRTlib.dll" _
(ByVal hwnd As Long, _
ByVal messageoffset As Integer _
) As String

But Im not sure whether it is OK since Im not including either the .lib or the .h files ?

HOw to proceed to steps 2 ) and step 3) above


also in the usage document they have said
/* You have to prepare to reserve window message id for the DLL for your program, */
/* and assign the DLL with MRT_Init() API. */
status = MRT_Init(hInstDLL, DLL_MESSAGE_OFFSET); // formulaic procedure.
MRT_Init(
HWND hWnd, // [in] Handle to window waiting a message.
UINT messageOffset // [in] Offset value of message ID.
);

(2) Parameters
hWnd
[in] An window handle receive the message.
messageOffset
[in] Specify the message ID which is the head value of range used by this library (DLL).

so what should I pass from visual basic to the MRT_init function for hWnd and messageOffset ?

What is reserving window message ?

What is Message offset ?


Please advise

chris
 
>and the steps given are

By whom are these steps given?

This is generally the C/C++ approach, but mostly isn't necessary in VB
 
Those steps come with the documentation which is vague at its best :)

If these steps are not needed in VB can you please let me know the correct way ?

 
In VB you generally don't need steps 1, 2 or 3

As for how the rest of your questions, without seeing the documentation for the dll in question it is difficult to answer them

 
Ok regarding the documentation

They have given function wise and for the INIT function

MRT_Init(): Initialize MRTLib.

(1) Function

The MRT_Init() function initializes DLL (MRTLib).

This should be called once before calling other functions

MRT_STATUS MRT_Init(
HWND hWnd, // [in] Handle to window waiting a message.
UINT msgOffset // [in] Offset value of message ID.
);
(2) Parameters
hWnd
[in] An window handle receive the message.
msgOffset
[in] Specify the message ID which is the head value of range used by this library (DLL).

(3) Return Values

MRT_STATUS

MRT_NORMAL: This function succeeds.

Please let me know if you need more information ?

so my questions are

1) How to get the current window handle in visual basic and pass it to the function ?

2) what is msgOffset ? How to get it in Visual Basic and pass it on to the function ?

3) The documentation says that "MRT_STATUS" must contain "MRT_NORMAL" ? so how to check for MRT_STATUS's valud using Visual basic ?
 
1) Form1.Hwnd will give you the window handle to Form1.
2) Just an integer number. There really isn't enough info (or, at least, clear enough info) in the documention you provide to say what specific value or range of values it might need to be
3) Without seeing the definition of MRT_NORMAL, which will likely be in the MRTlib.h file, this is tricky to answer specifically. Given a function declaration such as you provided earlier:

Private Declare Function MRT_Init Lib "C:\MRTlib.dll" (ByVal hwnd As Long, ByVal messageoffset As Integer) As String

It would be something like:

Result=MRT_Init(hwnd, msgOffset)
If Result=MRT_NORMAL Then
'Do your stuff here
End If
 
Just found out that all functions in the dll return

a structure like

typedef enum tag_MRT_STATUS {
MRT_NORMAL,
MRT_E_FATAL,
MRT_E_OVERFLOW,
} MRT_STATUS;

so for capturing the return value what datatype I must use in Visual basic ?

specifically

Private Declare Function MRT_Init Lib "C:\MRTlib.dll"

(ByVal hwnd As Long, ByVal messageoffset As Integer) As String --> ** The return value must be a C STRUCT equivalent **

The 'Status' variable is in fact a C STRUCT

so what is the equivalent of a C STRUCT in Visual basic ?

 
No it isn't, it is an enumerator, which is completely different (and exactly the same as a VB enum).

An enumerator, by default, is a list of integer constants that start with 0 (unless otherwise declared) and increment by 1. So

typedef enum tag_MRT_STATUS {
MRT_NORMAL,
MRT_E_FATAL,
MRT_E_OVERFLOW,
} MRT_STATUS;

Is an enumerator called MRT_STATUS, that contains
MRT_NORMAL = 0
MRT_E_FATAL = 1
MRT_E_OVERFLOW = 2

When C documentation suggests that a function returns an enumerator, what it actually means is that it returns one of the values of the enumerator. S in your example case, the documentation says that MRT_INIT function returns MRT-STATUS, which means it returns one of the three value, of which only one (MRT_NORMAL, which equals 0) indicates that the function has been successful

It's pretty similar in VB (and note that I'd change your function prototype in VB). Here's some example code to illustrate the point (although, given that I have no idea what MRT is or does it cannot be considered production code in any sense whatsoever):
Code:
[blue]Option Explicit

Private Enum MRT_STATUS
    MRT_NORMAL
    MRT_E_FATAL
    MRT_E_OVERFLOW
End Enum

Private Declare Function MRT_Init Lib "C:\MRTlib.dll" (ByVal hwnd As Long, ByVal messageoffset As Long) As MRT_STATUS

Private Sub Command1_Click()
    Select Case MRT_Init(Me.hwnd, 15)
        Case MRT_NORMAL
            MsgBox "MRT_Initialised OK"
        Case MRT_E_FATAL
            MsgBox "Fatal initialisation error"
        Case MRT_E_OVERFLOW
            MsgBox "Overflow problem encountered during intialisation"
        Case Else
            MsgBox "Unexpected value returned by MRT_Init"
    End Select
End Sub
[/blue]

 
Thanks for the excellent guidance I have been able to progress much far

The same dll expects a pointer to an array of STRUCT

Private Type MRT_List
version As Byte
instance As Long
speed As Integer
opbit As Integer
product As Integer
End Type

Dim somevar(1 to 10) as MRT_list

How do I pass a pointer to the array of an User defined datatype in visual basic ?

i.e I want to pass a pointer to the array "somevar" to a function

I have tried

Private Declare Function VarPtr Lib "msvbvm60.dll" Alias "VarPtr34" (Var() As Any) As Long

Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr34" (Var() As Any) As Long

Private Declare Function VarPtrStringArray Lib "msvbvm60.dll" Alias "VarPtr35" (Var() As Any) As Long

etc but it says data type mismatch

 
Just pass the first element of the array. You shouldn't need any of the Ptr stuff for this.
 
But The API says this

5.4. MRT_List(): Getting List of things
(1) Function
The MRT_List() function get the list of things , which have retrieved by MRT_Search().

MRT_STATUS MRT_List(
MRT_List* devList, // [in/out] Pointer to array of devList.
int* numberOfList // [in/out] The size of array.
);
(2) Parameters
*devList
[in] Specify the pointer to array of devList.

[out] Return the information of equipments retrieved.

numberOfList

[in] Specify the number of devList array size by reference.

[out] Return the number of equipments stored to *devList by this function.

(3) Return Values

MRT_STATUS
MRT_NORMAL: This function succeeds.


and in header file


typedef struct tag_MRT_List {
UCHAR version[VER_LEN];
DWORD instance;
int Speed;
int StopBit;
int product;
} MRT_List;

 
>But The API says this

I'm sure it does. But the way we pass a pointer to an array to an API call in VB is to pass the first element of that array ...

I won't cover passing structures (Types in VB) in this example, just the basics of passing an array (but Iwill use your function as a sort of prototype)

Declare Function MRT_List Lib "C:\MRTlib.dll" (FirstElement As Long, ByVal lElements As Long) As MRT_STATUS

and then you might have

Dim MRT_DemoList(1 to 10) as Long
Dim result as Long
result = MRT_list(MRT_DemoList(1), 10)

 
Thanks for all your help

Im constantly working on this application

Can you please tell me where I can learn more about passing structures (i.e Types in VB6) arrays to the dll ? Since I guess this dll will not accept anything but an array to a structure

Is there book or website etc ?

Thanks once again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top