Solved

Accessint C struct in VB?

Posted on 2001-08-07
6
180 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
[X]
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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

729 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