Solved

Visual basic 6 - linking to a dll file ?

Posted on 2006-06-25
17
1,476 Views
Last Modified: 2013-11-25
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 ?

also I have a query about how to retrieve the returned value from functions of this dlls

The document says for example

3) Return Values for the MRT_init function is

MRT_STATUS

MRT_NORMAL: This function succeeds.

i.e how do I find using visual basic whether the MRT_STATUS is MRT_NORMAL ??

Please advise

chris
0
Comment
Question by:christopher sagayam
  • 4
  • 4
  • 2
  • +2
17 Comments
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16981372
have u registered the dll u r using... unless n until u register it u cannot add reference to that dll..
First register the specific dll and try again.

http://www.filesland.com/companies/EMSA-SYSTEMS-LTD/Emsa-Register-Dll-Tool.html

Use this tool to register or unregister any dll. Its very easy.
0
 
LVL 6

Author Comment

by:christopher sagayam
ID: 16981396
I tried to register now but the get the error

.dll was loaded but the dllregisterserver entry point was not found
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 250 total points
ID: 16982586
First of all a regular windows dll doesn't need to be registered. If its not an activeX.dll than you will have to use the declerations to call one of the exported functions.
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 250 total points
ID: 16982607

>(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.
you gotta love vb,
it handles this crap


just use it like you should do with normal api,
for example:
MRT_Init _   me.hwnd, 15
0
 
LVL 6

Author Comment

by:christopher sagayam
ID: 16982637
Thanks for the me.hwnd tip

Ok I will use it for this function argument namely hInstDLL

can you please tell me what I should use for DLL_MESSAGE_OFFSET

The documentation says

 Specify the message ID which is the head value of range used by this library (DLL)

Can you please tell me what is a message offset and how can I get it using visual basic ?
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16982652

sorry, i can't help you with that....
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16982689
Well the return value from the looks of it is the returned status code. However I don't belive that its a return value of a string. This might be a long value.

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

Dim Status as long

Status = MRT_Init(me.hwnd,DLL_MESSAGE_OFFSET)

Now if you have the header file than you could look up the defined value of DLL_MESSAGE_OFFSET. This value has to be documented or you will never know how to call this exported function.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:justchat_1
ID: 16986810
most return values from dlls are longs...either zero or a positive number usually stands for success however it can vary.
Some also use specific return values to stand for constant strings...

As far as referecing the dll from vb, it may need to be compiled with a .def file
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16990157
U can try 1 thing..
I think the custom dll u r using might be many in number on ur pc. U might hav registered one of them and then it might hav been deleted by mistake. SO its entry is still in registry so u r getting error ' dllregisterserver entry point was not found'

DO one thing use Regmechanics or anyother registry checking software that removes invalid entries from registry. Then try again to register the dll u want to use. I am sure this time ur dll will surely register. Then u can add its reference to ur program.
0
 
LVL 6

Author Comment

by:christopher sagayam
ID: 16990215
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 Long --> ** The return value must be a C STRUCT equivalent **

Dim Status as long  --> It must be an equivalent of C STRUCT

Status = MRT_Init(me.hwnd,DLL_MESSAGE_OFFSET)

The 'Status' variable is in fact a C STRUCT

so what is the equivalent of a CSTRUCT in Visual basic ?
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16990895
The enum is just long values. You can either create the Enum in VB and use it within your declerations or you can just use a long variable.

'Where ? means the value of defined constant

public Enum My_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 Integer _
                     ) As My_Status


'Or simply just

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

Author Comment

by:christopher sagayam
ID: 16991173
Thanks for the help and Im able to progress now to the NEXT stage

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
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16996199

Dim m_List as MRT_List

then simply.

Varptr(m_List)
0
 
LVL 9

Expert Comment

by:justchat_1
ID: 17293846
correct answer given by egl1044
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now