0

I have a dynamic link library code that I created with C ++, like this

char* _stdcall encrypt(char *plaintext, char *key);

library name is ttvc.
this is the content of the *.def file

LIBRARY ttvc

EXPORTS
encrypt @1

I try to call this function from the vb code

Public Declare Function Lock Lib "ttvc.dll" Alias "encrypt"(ByVal plain as String, ByVal key As String)As String

and delphi code

function encrypt(plain:String;key:String;):PChar;stdcall;external 'TTVC.dll';

My code is working fine.

But I was confused, what is the difference between the string data types in Visual Basic, Delphi, and C++?
What is the difference between

  • String in VB
  • char* in C++
  • String in delphi
  • PChar in delphi

How can delphi and VB send strings to the library? How can the library receive strings from different programming languages and different data types?

3
  • 1
    Your code isn't working. You can't deallocate the string, and the .net code attempts to deallocate it using CoTaskMemFree. You can either have the caller allocate the output buffer, and use StringBuilder. Or use the COM BSTR. Commented Apr 12, 2015 at 6:25
  • 1
    @cahyo: Why do you use char in C++ and string in Delphi? It will work in non-unicode Delphi because the string is AnsiString and there is no conversion between PChar. But when you use unicode Delphi (2009+), string is UnicodeString. You should use the same type which is PAnsiChar. You can also search in stackoverflow ... stackoverflow.com/questions/237106/… Commented Apr 12, 2015 at 6:41
  • There are some problems with your question. You don't tell us anything about the lifetime management in the implementation. Knowing the types is never enough with interop. We also need to know the semantics of the argument passing. And then you ask for an explanation of string types in 3 different languages. There are about 5 different questions here. Commented Apr 12, 2015 at 12:29

1 Answer 1

2

From vb

a c string is passed as byval which passes the address of the first byte in the string. You must add a null byte (chr(0)) to the string as C strings use this to identify the end of the string (because the CPU does).

Com, and VB, uses Bstr. A Bstr is a C string, without a null terminating character, has a header containing it's length. Passing a Bstr you use ByRef which passes the address of the header.

In VB you can pass byte arrays by passing ByRef the first element of the array.

C Strings, BStrings, and Byte arrays can all contain string data. Most VB string functions work on byte arrays.

VB and COM are unicode. But VB was designed for Win95 which isn't. All strings are converted into ANSI for API calls (so files, strings, and all API calls are ANSI).

Use byte arrays to pass unicode to Windows unicode functions.

In Windows all functions that take ANSI strings are suffixed with A, and unicode suffixed with W. Functions that don't take strings don't have a suffix.

eg,

Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExW" (ByVal dwExStyle As Long, lpClassName As Any, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByVal lpParam As Any) As Long


   gRtfHwnd = CreateWindowEx(WS_EX_ACCEPTFILES + WS_EX_CLIENTEDGE, barray(0), "", Flags, 0, 0, ScaleX(Me.ScaleWidth, vbTwips, vbPixels), ScaleY(Me.ScaleHeight, vbTwips, vbPixels), Me.hWnd, vbNull, App.hInstance, vbNull)

Windows uses which CreateWindows was used to tell if the window wants ANSI or Unicode strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.