Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accessint C struct in VB?

Posted on 2001-08-07
6
Medium Priority
?
186 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

636 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