CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: String As Byte?

  1. #1
    Join Date
    Jan 2001
    Posts
    19

    String As Byte?

    Hi,
    I need to link vb code to a C dll, but I have a problem to convert one of the argument of a function.

    The function is declare like this in .pas:

    test(Filename as Byte)

    and like this in .h:

    test(char *Filename);

    Now I want to pass something like c:\test.txt in parameter, but I always get a run-time error '13' Type Mismatch.

    How can I convert a string in byte to pass ByRef to the dll?

    Thanks,
    Michael


  2. #2

    Re: String As Byte?

    U must declare arguments as string but use the option BYVAL.


    public declare Test lib "mylib.dll" (byval string filename)

    private sub command1_click
    dim Filename as string
    Filename="myfile.txt"
    test(Filename)
    end sub

    private sub command2_click
    test("myfile.txt")
    end sub






    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  3. #3
    Join Date
    Jan 2001
    Posts
    19

    Re: String As Byte?

    I can't change the declaration in .bas

    I found how to pass the string in bytes, but I got a new problem... Here is what I do:


    public declare Test lib "mylib.dll" (filename as Byte
    private sub command1_click
    dim Filename() as Byte
    Filename="myfile.txt"
    test(Filename(0))
    end sub




    My problem is that Filename does not contain myfile.txt, but contains m0y0f0i0l0e0.0t0x0t0. I have a string terminator after each byte, so the Test function only receive m, because the string is null terminated after it.

    How can I have myfile.txt in Filename?

    Thanks,
    Michael


  4. #4

    Re: String As Byte?

    but why not so?


    public declare Test lib "mylib.dll" (byval filename as string)

    private sub command1_click
    dim Filename() as string
    Filename="myfile.txt"
    test(Filename)
    end sub






    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  5. #5

    Re: String As Byte?

    using option byval...

    dim filename as string* 255
    filname="myfile.txt"
    test(filename)

    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: String As Byte?

    Give this a shot....


    public declare Test lib "mylib.dll" (filename as Byte)
    private Sub command1_click()
    Dim Filename() as Byte


    Filename = StrConv("myfile.txt", vbFromUnicode)
    Test (Filename(0))
    End Sub





  7. #7
    Join Date
    Jan 2001
    Posts
    19

    Re: String As Byte?

    That's what I did and it works.

    Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured