Solved

Passing a Char buffer from vb.net to C

Posted on 2004-08-09
6
1,216 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

806 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