passing ANSI strings to a COM object instance

Eddieb337

Member
Joined
Aug 6, 2003
Messages
5
Location
Bethesda, MD
Im upgrading an app from VB6 to VB .NET. The problem is i have smartcards with encrypted ansi strings on them. I pass these strings from the smartcard to the app to be decrypted.

In VB6:

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


This was a piece of cake using StrConv. In VB .NET I load the ANSI strings from the card but cannot pass them into the DecryptString method as an ANSI string. I can only pass them as Unicode strings which turns out to be a garbled mess when they are decrypted. I can only convert strCfr to an ANSI byte array but DecryptString will only take a string. I dont think i can Declare or use DllImport to make DecryptString an ansi function because it is not static but an instance method of TDciph321 which comes from TDCIPH32Lib. The question is how can i force DecryptString to behave as it should or force it to accept an ANSI string. Whats worse is that DecryptString works right on the parameter string you pass in, its an In/Out param. Thanks in advance to anyone who may be able to help.

Ed
 
Im not positive, but you might want to try changing the declare so that it accepts a byte array, and then passing the byte array that you retrieve from strCfr... Since a char* in C++ is really just a pointer to an array of chars (read: bytes), it may work. Its worth a shot, at least.
 
That makes sense but I dont think i can switch the declaration for the COM object. Thats easily done for static functions using DllImport but not for instance methods.
 
Oh, I see... its a COM object, not an API. Well, Im not exactly sure how you might go about doing it, then. Sorry. :-\ You might want to take a look at the System.Text namespace though. Theres a bunch of encoding conversion and stuff in there.
 
Ive been stuck on this problem for well over a week. Ive explored all the conversion stuff, it all converts to byte arrays.
Thanks for your comments though.
I dont know if this helps any but the upgrade wizard produced
Interop.TDCIPH32
 
have you tried an encoding conversion? eg:
Code:
Dim bt() As Byte = Encoding.ASCII.GetBytes("your string here".ToCharArray)
Dim strCon() As Byte = Encoding.ASCII.Convert(Encoding.UTF8, Encoding.ASCII, bt) /// convert to ascii.
MessageBox.Show(Encoding.Default.GetString(strCon))
 
Interop.TDCIPH32 is your runtime callable wrapper (RCW) generated from the type library that describes your COM DLL. Your best bet may be to modify the typelib to accept a byte array and then recompile the RCW manually using TlbImp.exe. Alternatively you could use TlbImp.exe to produce the RCWs IL which you could then modify to accept an ANSI string.
 
Maybe this will work:

Dim sUniCodeToConvert as String
Dim sAnsiString As String
Dim lbAnsiByte() As Byte

lbAnsiByte = System.Text.Encoding.Default.GetBytes(sUniCodeToConvert)

sAnsiString = System.Text.Encoding.Default.GetString(lbAnsiBytes)

...could do the trick. Perhaps.

Regards,

Goran
 
Back
Top