Solved

Passing a Char buffer from vb.net to C

Posted on 2004-08-09
6
1,221 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
[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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 3 132
maven disable workspace resolution 1 70
C qsort compare function issue 6 68
Eclipse integration with Github giving error 3 49
A short article about a problem I had getting the GPS LocationListener working.
This is about my first experience with programming Arduino.
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…

738 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