Results 1 to 6 of 6

Thread: LPSTR Strings in DLL

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    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;
    }
    }

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    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!

  4. #4

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60
    Oh, wait... It does work! Sorry, my fault... Thank muchly hairy dude

  5. #5
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    No Probs buddy.

    HD

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Just had a deja vu...
    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
  •  



Click Here to Expand Forum to Full Width