Sign in to follow this  
Followers 0
CoePSX

DllCall problem

17 posts in this topic

I made this little DLL, and compiled it under MinGW. It's very very simple, just rotates the bits of a string.

AutoIT calls the function, as it shows with the MessageBox, but it can't read the return value of it.

This is the DLL Code:

#include <windows.h>
#include <string.h>
#include <stdio.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

LPCSTR DLL_EXPORT EncryptString (LPCSTR, LPCSTR);
LPCSTR DLL_EXPORT DecryptString (LPCSTR, LPCSTR);
byte bitrotate (byte, int);

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    //
}

// Encrypt a string //
LPCSTR DLL_EXPORT EncryptString (LPCSTR sString, LPCSTR sPasswd) {
    MessageBox (NULL, sString, "Test", MB_OK);
    int iKey = 0;
    int i = 0;
    int j = 0;
    byte bTemp;
    char sFinal[strlen(sString)+1];

    // Calculates the key number from the password //
    for (i=0; i<strlen(sPasswd); i++) {
        iKey ^= sPasswd[i];
    }

    // Now rotate the bits acording to the key bits
    for (i=0; i<strlen(sString); i++) {
        bTemp = (char)sString[i];
        for (j=0;j<8;j++) {
            if (((byte)iKey & (1 << j)) != 0)
                bTemp = bitrotate(bTemp, 2);
            else
                bTemp = bitrotate(bTemp, 1);
        }
        sFinal[i] = bTemp;
    }
    sFinal[strlen(sString)] = '\0';

    MessageBox (NULL, sFinal, "Test", MB_OK);
    return (LPCSTR)sFinal;
}

// Decrypt a string //
LPCSTR DLL_EXPORT DecryptString (LPCSTR sString, LPCSTR sPasswd) {
    int iKey = 0;
    int i = 0;
    int j = 0;
    byte bTemp;
    char sFinal[strlen(sString)+1];

    // Calculates the key number from the password //
    for (i=0; i<strlen(sPasswd); i++) {
        iKey ^= sPasswd[i];
    }

    // Now rotate the bits acording to the key bits
    for (i=0;i<strlen(sString);i++) {
        bTemp = (char)sString[i];
        for (j=0;j<8;j++) {
            if (((byte)iKey & (1 << j)) != 0)
                bTemp = bitrotate(bTemp, -2);
            else
                bTemp = bitrotate(bTemp, -1);
        }
        sFinal[i] = bTemp;
    }
    sFinal[strlen(sString)] = '\0';

    return (LPCSTR)sFinal;
}

byte bitrotate (byte iByte, int iTimes) {
    byte iTemp;
    int i;

    if (iTimes > 0) {
        for (i=0;i<iTimes;i++) {
            iTemp = iByte & 1;
            iByte >>= 1;
            if (iTemp==1)
                iByte |= 128;
            else
                iByte &= 127;
        }
    }
    else if (iTimes < 0) {
        for (i=0;i>iTimes;i--) {
            iTemp = iByte & 128;
            iByte <<= 1;
            if (iTemp==128)
                iByte |= 1;
            else
                iByte &= 254;
        }
    }
    return iByte;
}

And the AutoIT code:

$DLL = @ScriptDir & "\coecrypt.dll"

$Encrypted = DllCall($DLL, "str", "EncryptString", "str", "test string", "str", "password")
MsgBox(0, "DEBUG", $Encrypted)

I'd be glad if anyone could help me, cause i have no idea what to do now :) .

Thanks in advance.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites



$Encrypted[0] = "$)D"

$Excrypted[1] and $Encrypted[2] are the arguments, "test string" and "password"

That already helps me alot! :) Now I only need to uderstand why $Encrypted[0] is so strange... but i'll mess with it to see if i get it.

Thank you very much Larry!!


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

With any string it gives me always "$)D", and the length is always 3.

=/


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Thanks! I'll try to remove the nulls.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

I fixed the problem by doing this:

// Encrypt a string //
const char* DLL_EXPORT EncryptString (const char* sString, const char* sPasswd) {
    //MessageBox (NULL, sString, "Test", MB_OK);
    int iKey = 0;
    int i = 0;
    int j = 0;
    byte bTemp;
    char* sFinal;

    // Calculates the key number from the password //
    for (i=0; i<strlen(sPasswd); i++) {
        iKey ^= sPasswd[i];
    }

    // Now rotate the bits acording to the key bits
    for (i=0; i<strlen(sString); i++) {
        bTemp = (char)sString[i];
        for (j=0;j<8;j++) {
            if (((byte)iKey & (1 << j)) != 0)
                bTemp = bitrotate(bTemp, 2);
            else
                bTemp = bitrotate(bTemp, 1);
        }
        sFinal[i] = bTemp;
    }
    sFinal[strlen(sString)] = '\0';

    return sFinal;
}

Without the NULL at the end it would return strange chars after the end of the string.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Just out of curiosity, why would you make an actual DLL file when you could make an AutoIt Plugin DLL that would I think make things a bit easier in the your code.

Of course this is coming from someone that hasnt yet used DLLCall() :)

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Why haven't you used DLLCall?

An AutoIt Plugin DLL is an actual DLL.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ahm... I'm trying to learn C++. That was just an attempt to learn to make DLLs. :)

Just a little stupid question off-topic. Actually two...

What's the difference between s and sz in variable prefixes, like sName and szName?

And why is there "2 ^ 128" in IceKirby1's sig, when 2 ^ 128 means 2 BitXOR 128?

Edited by CoePSX

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Why haven't you used DLLCall?

An AutoIt Plugin DLL is an actual DLL.

Actually havent seen the need in any of my scripts yet. I havent done anything crazy or off the wall I guess. Just using built in functions, and built my own plugin when I needed File/String Hashes (MD5, SHA-1)

Yea I know an AutoIt Plugin is an actual DLL, I have created one. They are a bit easier to use in the syntax in AutoIt though.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

What's the difference between s and sz in variable prefixes, like sName and szName?

I don't think there is any difference.

But i usually use sz for C++ null-terminated strings and str for the std library strings.

And why is there "2 ^ 128" in IceKirby1's sig, when 2 ^ 128 means 2 BitXOR 128?

That's not the C++ operator ^. That's the mathematical operator ^. It means to the power of.

#)

Share this post


Link to post
Share on other sites

I don't think there is any difference.

But i usually use sz for C++ null-terminated strings and str for the std library strings.

That's not the C++ operator ^. That's the mathematical operator ^. It means to the power of.

#)

What does the z stand for? The sound of "strings" being "stringz"?

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Using sz means the string is zero-terminated. It's a C-style string. Using just "s" means the string is probably not zero-terminated. For example, if you were to use MFC's CString class or STL's std::string, you would not use szName but rather sName.

Search for "Hungarian Notation" for more information on the prefixes and what they generally mean.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

XD that's why i think 'sz' so weird.

Edit: Thanks Valik, hadn't seen your post when i posted.

Edited by CoePSX

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Using sz means the string is zero-terminated. It's a C-style string. Using just "s" means the string is probably not zero-terminated. For example, if you were to use MFC's CString class or STL's std::string, you would not use szName but rather sName.

Search for "Hungarian Notation" for more information on the prefixes and what they generally mean.

Thanks Valik! I appreciate it. I will certainly do that.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Yes, I am using the BASIC ^ to-the-power-of symbol, not the XOR symbol. Why did you ask that way? Why not ask "Icekirby1, why is that symbol in your sig" instead of making a reference to me in what I think is the second person.

I've always wondered what sz meant. I rarely use it in my own code, but no one really sees it so it doesn't matter.

Share this post


Link to post
Share on other sites

Sorry about that Icekirby1. It was just a way of asking.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0