Solved

Passing a Char buffer from vb.net to C

Posted on 2004-08-09
6
1,212 Views
Last Modified: 2008-02-01
Hey,

    I have a VB program that calls on an older c DLL to fill a 4k in size char buffer.  I was wondering what is the best way to do this...I have made several failed attemps...:)  Not really famillar with the managed vs unmanaged code yet...so i am sure i'm missing something simple:)

dasid is a enumerated number for a file
The unisgned char *buf is returned with the data needed in vb program
int len is the size of char buf you are passing in
int wave is not used i just pass in a empty intptr

C code--------------------------------------------------------------------------------
 EXPORT int das_get_wave(int dasid, unsigned char *buf, int len, int *wave);
VB code-------------------------------------------------------------------------------

        Dim arraywavedata_length As Integer = 4096
        Dim wave As IntPtr
        Dim arraywavedata(4096) As String
        Dim arraywavedata_intptr As IntPtr = Marshal.StringToHGlobalAnsi(arraywavedata(4096))

        Public Declare Function das_get_wave Lib "libdas.dll" (ByVal dasid As Integer, ByVal buf As IntPtr, ByVal len As Integer, ByVal wave As IntPtr) As Integer

my intptr to arraywavedata returns 0...
0
Comment
Question by:computerfixins
6 Comments
 
LVL 3

Assisted Solution

by:aravindtj
aravindtj earned 50 total points
ID: 11754383
Hai,
 I think that in the C function call das_get_wave gives only the integer value.
 But you are getting the pointer in VB call. So, you may get garbage value. Try to return  the pointer in das_get_wave function.
All the Best.
:)
0
 
LVL 2

Assisted Solution

by:newwares
newwares earned 50 total points
ID: 11754547
Hi,
Maybe you can try P/Invoke. Here is an example in manged c++:
http://www.codeproject.com/managedcpp/pinvoke.asp

But you can convert it to Visual Basic .Net.
0
 
LVL 7

Author Comment

by:computerfixins
ID: 11754870
Hey,

Forgot this line of code...should make it easier to read....

C code--------------------------------------------------------------------------------
 EXPORT int das_get_wave(int dasid, unsigned char *buf, int len, int *wave);
VB code-------------------------------------------------------------------------------

        Dim arraywavedata_length As Integer = 4096
        Dim wave As IntPtr
        Dim arraywavedata(4096) As String
        Dim arraywavedata_intptr As IntPtr = Marshal.StringToHGlobalAnsi(arraywavedata(4096))

***       arraywavedata_count = das_get_wave(dasid, arraywavedata_intptr, arraywavedata_length, wave)  ***

VB.mod----------------------------------------------------------------------------------
        Public Declare Function das_get_wave Lib "libdas.dll" (ByVal dasid As Integer, ByVal buf As IntPtr, ByVal len As Integer, ByVal wave As IntPtr) As Integer


0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 19

Accepted Solution

by:
drichards earned 400 total points
ID: 11756602
Is the result supposed to be a String?  If so, you can use code from the 'Declare' docs at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmDeclare.asp (code shown below).  Your code is creating an array of NULL String references and trying to marshal one of the NULL references.  That will not work.  You just need a single String with an allocated buffer, and according to the sample, you don't need to explicitly marshal.   The sample creates a 25 character string so you'll have to change that to your desired length.
-----------------------------------------------------------------------------------------------
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Function GetUser()
   Dim RetVal As Integer
   Dim UserName As String
   Dim Buffer As String
   Buffer = New String(CChar(" "), 25)
   RetVal = GetUserName(Buffer, 25)
   UserName = Strings.Left(Buffer, InStr(Buffer, Chr(0)) - 1)
   MsgBox(UserName)
End Function
-----------------------------------------------------------------------------------------------

If you really want a buffer of unsigned char, you should create a Byte array and pass a pointer to it.  Otherwise, you get unwanted conversions happening since .NET strings are UNICODE.
0
 
LVL 7

Author Comment

by:computerfixins
ID: 11757390
The values that are returned in  ByVal buf As IntPtr are from 0-255 so a buffer of unchar would work....

How do you create a pointer too a byte array?

Do you have too marshal the byte array since your going from managed code to unmanaged>?

Will the .net garbage collectors move my pointer on me>?....

Also any good books that scope going from vb.net to older C dll's....that way i wont keep asking newb questions:)



0
 
LVL 7

Author Comment

by:computerfixins
ID: 11761899
Hey,

Found a good write up on pointers and vb.net here

http://www.fawcette.com/Archives/premier/mgznarch/vbpj/2001/10oct01/rt0110/rt0110p.asp

Thanks for pointing me in the right direction...:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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