Learn how to a build a cloud-first strategyRegister Now


Dealing with pointers calling into a C++ DLL from Visual Basic (VB6)

Posted on 2003-11-03
Medium Priority
Last Modified: 2007-12-19
I have a C++ DLL formatted correctly to allow me to call into it from VB. The functions are declared in the module etc fine but the problem I am having is dealing with the pointers C++ uses.

One example with void* which is meant to be casted into a specific user defined type in C++.
     void* deStateIn (int *size)
How can I handle this in VB? If I had a void* as a paramter how would I pass something into it from VB?

How do I deal with a pointer to a user defined object being returned?
     diueStatusType* deCmdUpdateStatus()

How do I deal with a char* being returned?
     char* deStateSite (int x, int y)

How do I deal with a method that takes a pointer to a unsigned char that is really a buffer for a string? (expecting something simliar to unsigned char ipaddr[16] = "")
     int diuInit (int max_slots, unsigned char*dest_ip, int udp_port)

Any help would be greatly appreciated. I have been trying to solve these issues for awhile and am not having any luck. Thanks in advance.

Question by:icode4food
  • 5
  • 5
LVL 28

Expert Comment

ID: 9674488
1) I think you can use the "As Any" declaration type

2) If you are talking about an internal class, I don't think you can pass pointers of it to and from VB unless it is an ActiveX object or a struct (user-defined type).

3) For char you would declare "As Byte" in VB

4) char pointer buffer for a string would be a byte array in VB, e.g.,   Declare SomeFunction Lib "mydll" (ByVal AVariable() As Byte)

I don't do any C++ programming so these are just speculated answers...

Author Comment

ID: 9680802
Thanks for the suggestions but
1) doesnt work. As Any cant be a return type on a DLL function declaration
2) I was told there is a way to get at the memory and overlay it but I havent had luck doing so.
3) doesnt work. if I wanted to assign "" as the IP address I cant store it into a Byte array. An assignment that isnt a number into a Byte field does not work. Its a Type Mistmatch. Tried using CByte method as well and that won't work either.
4) see 3
LVL 28

Expert Comment

ID: 9681024
Just a char should correspond to a byte, and yes, you would want to return the ASCII value of that character into your byte.  A char * should correspond to a byte array, with the same limitations I just mentioned.

Regarding the memory overlay, etc...it seems an awfully unorthodox method of working in the program.  I would consider finding an ActiveX approach (the best way to work with objects between the two languages) or handle all class functions internally, using exported functions to allow VB to manipulate the properties and use the class.

If interested, this is the *proper* way to pass string to and from your dll to VB:

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 9681380
Was able to use Asc() to get the ascii value put into the byte. Whether the dll is able to handle this appropriately I can't tell yet without being able to resolve my other issues. I competely agree this program is "unorthodox" but the client keeps pushing for it even though I keep telling him I don't think its possible.
LVL 28

Expert Comment

ID: 9681562
Can you use As Variant as a return type?  What exactly are you trying to return?  If its just a pointer, then As Long should do it.

What other issues are still outstanding?

Author Comment

ID: 9681804
If I do it as a Variant return type and try to store it on return I get "Bad DLL calling convention."
        Dim myVariant As Variant
        myVariant = deStateIn(sizeRet)
deStateIn returns a void*. In normal use it is actually returning a pointer to a C++ user defined object, which is what I need to get at. If I did return the value as a long since its just a pointer then I need a way to dereference it like you would in C++ which is not an option in VB.
LVL 28

Expert Comment

ID: 9681833
You can do a trick to dereference an object pointer back into an object in VB, but VB must be aware of this object (e.g., be able to declare a variable of that type).

Author Comment

ID: 9682051
I already have a type set up in VB to mimic the structure of the C++ type I am expecting. Its a rather long structure so I cut most of it, and due to security issues.
*** VB Code ***
Public Type pduEntityStateBaseType
   version              As Byte
   exercise             As Byte
   type                 As Byte
   protocol_family      As Byte
   time_stamp           As Long
   length               As Integer
   entitystate_pad2(2)  As Byte
End Type

*** C++ Code ***
typedef struct {
     UINT8      version;        
     UINT8      exercise;
     UINT8      type;
     UINT8      protocol_family;
     UINT32      time_stamp;
     UINT16      length;
     UINT8      entitystate_pad2[2];
} pduEntityStateBaseType;

I know windows has a MSVBVM60.DLL which has calls such as GetMem4 which would get a long (32-bit) value at a supplied location.

' MSVBVM60.DLL functions
Public Declare Sub GetMem1 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Byte)
Public Declare Sub GetMem2 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Integer)
Public Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Long)
Public Declare Sub GetMem8 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Currency)
Public Declare Sub PutMem1 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Byte)
Public Declare Sub PutMem2 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Integer)
Public Declare Sub PutMem4 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Long)
Public Declare Sub PutMem8 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Currency)

Dim vptr As Long
Dim newvalue As Long
' get the long (32-bit) (4-byte) value at the location (same as a C dereference of a pointer)
Call GetMem4(vptr, newvalue)

Problem is these gets arent able to get something large enough for the object I have. I was told to try using these get calls and overlay the information on the user defined object but I can't get that to work either.
LVL 28

Accepted Solution

AzraSound earned 520 total points
ID: 9682137
Well, here is the VB hack at dereferencing:

Its in the sample project entitled "Resolving Pointers Demo.zip".  I dont know if it works with types, however.  With that said, it seems the simple CopyMemory API should work if you know exactly the memory address you are working with, and what offsets are needed to set the necessary information.  Obviously, if we can resolve the pointer into a variable of the user defined type, that would be ideal.

Author Comment

ID: 9869243
For others that have to deal with pointers try this site. It gives good examples of using CopyMemory.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

810 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