-
LPSTR Strings in DLL
I have a VC++ DLL function that should be called from VB. The function Encrypts a string (It's more complex than the one shown). This following code, however, returns the exact same string, can anyone see whats wrong?
From VB, the function is called like this
a$="Test String"
EncryptString(a$)
DLL Function
void _stdcall EncryptString(LPSTR data) {
for (int i=0; i<strlen(data); ++i) {
data[i]^=5;
}
}
-
Because the data is not passed 'By reference'. VB automatically passes vars by ref, but C/C++ does it by value.
Code:
void _stdcall EncryptString(LPSTR &data) {...}
This should work fine. The '&' tells C++ that the variable will be passed by reference and therefore any local manipulation is done on the actual string - and not some stack variable.
In C this has to be done by dereferencing the variable:
Code:
void _stdcall EncryptString(LPSTR *data)
{
for(int i=0; i < strlen(data); ++i)
{
*data[i]^=5;
}
}
Nasty eh?
HD
-
I doesn't seem to make any difference... I'll try to describe it better:
The function declaration in VB looks like this
Private Declare Sub EncryptString Lib "Encryptor.dll" (ByVal Data As String)
I have tried both ByVal and ByRef for this.
This is how I call the DLL Function
a$="Test String"
EncryptString(a$)
MsgBox a$
The result is a message box reading "Test String"
The DLL Function looks like this
void _stdcall EncryptString(LPSTR data) {
long dataLen=strlen(data);
for (long i=0; i<dataLen; ++i) {
data[i]^=5;
}
}
When I use "LPSTR &data" I get an error when using the strlen function, an access violation.
I realise the encryption is bad, but it's only a test encryptor.
Someone please help!
-
Oh, wait... It does work! Sorry, my fault... Thank muchly hairy dude
-
-