Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Accessint C struct in VB?

Posted on 2001-08-07
6
177 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

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 28

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

808 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