Click here to Skip to main content
15,867,832 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Ladies and Gentlemen of Code Project,

I am trying to figure out how to send and receive strings from a function written in C++ (using Visual Studio 2015) to VB6.

Please be aware that the strings will be sent and received from the argument list of the function

I am experienced with VB6, but not C++!

The declaration is as follows:

VBDLLFUNC(int) MDNSa35dbd7v2(int service, int AC0_CryptoKeyTable, unsigned char* seed, unsigned char* key);

The code/wrapper is as follows:

VBDLLFUNC(int) MDNSa35dbd7v2(int service, int AC0_CryptoKeyTable, unsigned char* seed, unsigned char* key) {
return (a35dbd7v2(service, AC0_CryptoKeyTable, seed, key));
}

Where #define VBDLLFUNC(TypeName) DLLEXPORT TypeName __stdcall

I have the following declaration in my VB6 code:

Declare Function MDNSa35dbd7v2 Lib "MDTestDLL.dll" (ByVal service As Long, ByVal
CryptoKeyTable As Long, ByVal Seed As String, ByRef Key As String) As Long

In my Form Load event I call the function as follows:

Private Sub Form_Load()

Dim lRetVal As Long
Dim sKey As String
Dim sSeed As String

The customer is telling me that if call MDNSa35dbd7v2 with a sSeed = 5 bytes of 11 (I don't know if they mean 11 dec or B hex) as the sSeed

sSeed = Chr(11) & Chr(11) & Chr(11) & Chr(11) & Chr(11)

I should get back 5 bytes of 22 for sKey

lRetVal = MDNSa35dbd7v2(&H1, &H3C, sSeed, sKey)

End Sub

The question that I have is this, since I am dealing with VB6 then the functions declaration and the function itself should maybe be declared as follows:

VBDLLFUNC(int) MDNSa35dbd7v2(int service, int AC0_CryptoKeyTable, BSTR seed, BSTR
key);

VBDLLFUNC(int) MDNSa35dbd7v2(int service, int AC0_CryptoKeyTable, BSTR seed, BSTR key) {
return (a35dbd7v2(service, AC0_CryptoKeyTable, seed, key));
}

Please also be aware that the header declaration for a35dbd7v2 is as follows:

extern __declspec(dllexport) int a35dbd7v2(int service, int AC0_CryptoKeyTable, unsigned char* seed, unsigned char* key);

Since I am calling this funvtion from VB6 and VB6 uses BSTR when dealing with strings, then how would I convert BSTR to unsigned char*?
Please advise

Thank you

What I have tried:

I have been googling and think that I might be close
Posted
Updated 27-Nov-19 7:41am

1 solution

You already received suggestions in your original question at How to return a string from a DLL written in C++ to VB6[^]. If you need clarification then please use the “Have a Question or Comment” link beneath an answer. Please do not repost.
 
Share this answer
 
Comments
gary hagerty 27-Nov-19 13:47pm    
How would I go about converting a BSTR to an unsigned char*
Richard MacCutchan 27-Nov-19 13:52pm    
Look at the BSTR documentation: BSTR | Microsoft Docs[^].
gary hagerty 27-Nov-19 14:28pm    
Hello Sir,

Thank you very much for your reply, however the link did not provide me with any details on how to convert a BSTR string to an unsigned char* and then convert unsigned char* back to BSTR so it can be returned to VB6.

Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900