Jump to content

Dll Call Help


 Share

Recommended Posts

I made a dll in c++ that return a string,but,when i use dllcall it returns in pointer,i dont know if its Autoit problem or dll !

Please help !

C++ Function:

char* samplefunction()
{
    char* str1 = "Hello";
    char* str2 = "bye";
    char result[strlen(str1) + strlen(str2) + 2];
    sprintf(result,"%s-%s",str1,str2);
    return (char*)result;
}

Autoit Function

$hRet = DllCall('test.dll',"ptr","samplefunction")
MessageBox(0,"Test","Return : "&$hRet[0])

The dll is successfully built and function is executed except it doesnt return a string.

Thanks in advance !

test.rar

Edited by DrH
Link to comment
Share on other sites

try:

$hRet = DllCall('test.dll',"str","samplefunction")
MsgBox(0,"Test","Return : "&$hRet[0])

You are trying to return string but your's code expects pointer.

In autoit there's no MessageBox, use MsgBox instead.

also use @error in msgbox to see if there are any errors with call

MsgBox(0,"error",@error)

hope it helps

edited

Link to comment
Share on other sites

And, depending on your OS and compile options, you could even have more chance with:

$hRet = DllCall('test.dll', "str:cdecl", "samplefunction")
MsgBox(0,"Test","Return : "&$hRet[0])

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

And, depending on your OS and compile options, you could even have more chance with:

$hRet = DllCall('test.dll', "str:cdecl", "samplefunction")
MsgBox(0,"Test","Return : "&$hRet[0])

It returns an invalid char "",btw,i use codeblocks with "GNU GCC Compiler",any recommendation ?

Thanks for your help !

Link to comment
Share on other sites

C or C++ compile, 32- or 64-bit, which GCC options?

Recommending anything out of the blue isn't going to do wonders.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Forgive me if I'm wrong, but is that not an array of char, and the address of index 0 that is returned?

And if so, would you not need to read the memory at that address, or DllStructGetData?

EDIT:

I do remember trying this using a dll at one point, and if my memory serves me I used LPSTR as the C++ return type, and that worked fine with autoit "str" type in the call.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Forgive me if I'm wrong, but is that not an array of char, and the address of index 0 that is returned?

And if so, would you not need to read the memory at that address, or DllStructGetData?

EDIT:

I do remember trying this using a dll at one point, and if my memory serves me I used LPSTR as the C++ return type, and that worked fine with autoit "str" type in the call.

When i use ptr it returns in $hRet[0] = a pointer,when i use str it returns an invalid char !

- How to read that memory address ?

Thanks in advance !

Link to comment
Share on other sites

check if your project is unicode. it this case it may return wstr instead of str.

if it really return pointer you need

dllstructgetdata(DllScructCreate("char",$hRet[0]),1)

Not working,but thanks !
Link to comment
Share on other sites

Yhere were problems with your C++ you were returning pointer to local variable but local variable gets deleted on return.

C++ needs to be like this

LPWSTR  DLL_EXPORT samplefunction()
{
    LPWSTR result = L"This is a test";
    return result;
}

and autoit should look like this

$hRet = DllCall('bin\debug\000.dll','wstr:cdecl','samplefunction')
If @Error Then MsGBOx(0,'Error','@Error : ' & @Error)
MsGBOx(0,'Return','Return String : ' & $hRet[0])

Here's your new code

000.zip

Edited by E1M1

edited

Link to comment
Share on other sites

Yhere were problems with your C++ you were returning pointer to local variable but local variable gets deleted on return.

C++ needs to be like this

LPWSTR  DLL_EXPORT samplefunction()
{
    LPWSTR result = L"This is a test";
    return result;
}

and autoit should look like this

$hRet = DllCall('bin\debug\000.dll','wstr:cdecl','samplefunction')
If @Error Then MsGBOx(0,'Error','@Error : ' & @Error)
MsGBOx(0,'Return','Return String : ' & $hRet[0])

Here's your new code

000.zip

You are adding additional confusion.

If you don't understand what the problem is (with DrH's code) then at least learn to shut the fuck up and sustain yourself from commenting.

Link to comment
Share on other sites

I was just trying to help. why do you have to be that hard with me? differently from you I at least gave working code. I am not saying anything bad about you just mentioning that you at least could have let him decide wether it's helpful or not. That's just 1 possible solution of many.

learn to shut the fuck up

You are MVP and using such impolite expression when there are more polite ways saying this. Do you have something against me? Edited by E1M1

edited

Link to comment
Share on other sites

Maybe you wouldn't comprehend otherwise. You failed to comprehend other said things.

You could have said "Stop making things worse" or something like that, and that would have been with out that expression.

Wat makes you think that you wouldn't comprehend otherwise? and why do you think that "fuck" makes it more clear? That doesnt work with me. Instead 1 polite sentence would have worked like a charm (at least with me).

DrH allow me to ask. Did I add additional confusion?

Edited by E1M1

edited

Link to comment
Share on other sites

Calm down both of you !

Each one of you want just to give me some help,maybe trancexx was a little bit hard on you ,but shes just trying to give me some help !

E1M1 ,in the c++ source ,i want to use sprintf and strcat not returning a string like you did!

Again trancexx just wanted to help she didnt mean it !

Thank you fellas i appreciate !

Edited by DrH
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...