|
-
Aug 6th, 2003, 01:02 PM
#1
Thread Starter
New Member
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
-
Aug 6th, 2003, 05:11 PM
#2
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:
[color=blue]Private Function[/color] asciString([color=blue]ByRef[/color] strString [color=blue]As String[/color])
[color=blue]Dim[/color] cr() [color=blue]As Char[/color] = strString.ToCharArray
[color=blue]Dim[/color] s() [color=blue]As Byte[/color] = Encoding.ASCII.GetBytes(cr)
strString = Encoding.ASCII.GetString(s)
[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]
-
Aug 7th, 2003, 09:11 AM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|