Results 1 to 3 of 3

Thread: ANSI strings in VB .NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    ANSI strings in VB .NET

    The problem is I have an instance of an object from a COM that has a method that takes a string ByRef. The actual string that gets passed in gets manipulated by the method. The code worked fine in vb6 when string conversion to different formats were available using vbFromUnicode and vbUnicode. .NET only allows unicode strings. You can convert a string to ansi but what you get back is a byte array not a string. The method only accepts strings. DllImport and Declaring the function as ansi doesn't work because this is an instance method not a static method. Does anyone know a work around for this? I really need to pass an ansi string into this method but all i can pass is a unicode sting.

    ex:

    Dim foo as someComObject
    foo.method(myString) ' unicode when it should be ansi

    Thanks in advance.

    Ed

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i'm not quite sure what the object is you are referencing to thats causing the problem , but maybe you could use the Encoding method, eg:
    VB Code:
    1. [color=blue]Private Function[/color] asciString([color=blue]ByRef[/color] strString [color=blue]As String[/color])
    2.         [color=blue]Dim[/color] cr() [color=blue]As Char[/color] = strString.ToCharArray
    3.         [color=blue]Dim[/color] s() [color=blue]As Byte[/color] = Encoding.ASCII.GetBytes(cr)
    4.         strString = Encoding.ASCII.GetString(s)
    5.     [color=blue]End Function[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    forcing an instance method to take an ANSI string

    The problem with Encoding.Ascii.GetString is the string it returns will still be a unicode string. foo.method(String) takes a string but the string must be an ANSI string. You can declare COM functions to be ANSI but only for static functions not instance methods such as foo.whateverMethod. The reason why I need to do this is that foo.method is actually a decryption method. It needs to decrypt legacy ANSI strings. I'm upgrading (dont' ask why) this app from vb6 to vb .NET. In vb6 ansi strings were ok. You could just use StrCnv(vbFromUnicode). This isn't an option in .NET. Here is the actual code from the VB6 app.


    strCfr = StrConv(strCfr, vbFromUnicode)
    status = TDciph321.DecryptString(strCfr, LenB(strCfr))

    Thanks in advance for any help anyone could provide.

    Ed

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