Sign in to follow this  
Followers 0
DrH

Dll Call Help

22 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
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)

Share this post


Link to post
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 !

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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.

Share this post


Link to post
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.

Im using C++ 32,default options in codeblocks !

Share this post


Link to post
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 !

Share this post


Link to post
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)


edited

Share this post


Link to post
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 !

Share this post


Link to post
Share on other sites

can you attach your dll compiled as file? I could try hack it until i get script for you


edited

Share this post


Link to post
Share on other sites

can you attach your dll compiled as file? I could try hack it until i get script for you

Done !

Thank you,you're helpful !

Share this post


Link to post
Share on other sites

This is not AutoIt related.

You need to work on your c++. Read about dynamic allocation.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

This is not AutoIt related.

You need to work on your c++. Read about dynamic allocation.

I readed about it but i didnt understood !

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

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

Share this post


Link to post
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.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

You are MVP and using such impolite expression when there are more polite ways saying this.

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

Do you have something against me?

Who are you? <-this is not a question

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

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

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