|
-
Nov 2nd, 2002, 11:31 AM
#1
Thread Starter
Member
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;
}
}
-
Nov 2nd, 2002, 07:32 PM
#2
Addicted Member
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
-
Nov 3rd, 2002, 08:06 AM
#3
Thread Starter
Member
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!
-
Nov 3rd, 2002, 08:22 AM
#4
Thread Starter
Member
Oh, wait... It does work! Sorry, my fault... Thank muchly hairy dude
-
Nov 3rd, 2002, 01:04 PM
#5
Addicted Member
-
Nov 4th, 2002, 12:57 PM
#6
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|