Solved

Accessint C struct in VB?

Posted on 2001-08-07
6
171 Views
Last Modified: 2008-03-10
I have a VB compiled app, that uses a DLL written in C.  The VB app calls a function in the DLL to register a callback function in the VB app.  The DLL then calls the VB Callback function periodically with a pointer to a data structure.  Everything works fine with the initial register call and the periodic callback works fine also... I am getting the correct address as the only parametyer of the callback function...

I just need some help with the last step... how can I access the members of this structure in VB now that I have th address in VB?  Is there a VB equivalent of the C structure that I can cast to this address and then access the structure members?

Thanks,
Jeff
0
Comment
Question by:jeffs1
6 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 6359253
I have seen some code which does this using the copymemory API call.

e.g.

Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


DIM Fred as YourStructure

and when you receive the address:

CopyMemory Fred, YourAddressFromDLL, Len(fred)

YOU WILL GET PROBLEMS IF YOU INTEND TO HAVE STRINGS IN YOUR STRUCTURE.  If you need strings you will need to unpack them into another place before you use them.

Hope this works for you.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6359273
You'd need to define the same structure in Visual Basic. Define a variable of that type, and use that variable to retrieve the correct items.

Another thing you can do is by creating a public struct / class in the C dll, and create a variable of that type in your VB program...

regards
CJ
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6359474
hearing...
0
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

 
LVL 27

Expert Comment

by:Ark
ID: 6361891
Hi

Public Type MyStruct
    Member1 As Long
    Member2 As Integer
    Member3(1 to 15) As Byte
    Member4 As String
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)

Public Function MyCallBack (ByVal lpStruct As Long) As MyStruct
   Dim ms As MyStruct
   Call CopyMemory(ms, ByVal lpStruct, Len(ms))
   MyCallBack = ms  
End Function

Cheers
0
 

Author Comment

by:jeffs1
ID: 6361973
OK... that makes sense... one question though...

I think I understand that the Member3(1 to 15) As Byte would equate to "char field3[15]" on the C side... but what would the Member4 As String look like on the C side? From what I understand VB uses Unicode for Strings... What is the best way to pass C "string" info (char array) to VB? Keeping in mind that this DLL needs to be used by C and VB applications... so the same callback prototype should work with both...

Thanks,
Jeff
0
 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
ID: 6362249
Hi
Can you show you C structure? I'll convert it to VB. There is difference in strings in VB and C. Take a look at Bruce McKinney explanation (BTW, "must have" book for VB + API programming).
See http://www.mvps.org/vb/hardcore/
Chapter 2 -> "The API Contract" and "Dealing with Strings"

Cheers
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

707 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

11 Experts available now in Live!

Get 1:1 Help Now