Sign in to follow this  
Followers 0
jaberwacky

Calling DLL functions...

8 posts in this topic

#1 ·  Posted (edited)

Hi, I'm new to using and making DLLs and so far I made an example DLL and I have filled it with three functions. The functions return an int, float, and char respectively. I can DLLCall the int and float functions but not the char! I'm not sure what's going on. String doesn't seem to work for me either. My search through the forum didn't have any examples which used char. I included the DLL code too just in case. Any thing you throw my way will be surely appreciated.

Global $hDll = DllOpen("D:\CodeBlocks\C++ Programs\Line Alg DLL\bin\Debug\Line Alg DLL.dll")

Global $return = DllCall($hDll, "int:cdecl", "Add", "int", 6, "int", 4) ; <-- works
ConsoleWrite("@error: " & @error & " Returns: " & $return[0] & @CRLF)

Global $return1 = DllCall($hDll, "float:cdecl", "AddF", "float", 5.5, "float", 4.5) ; <-- works
ConsoleWrite("@error: " & @error & " Returns: " & $return1[0] & @CRLF)

Global $return2 = DllCall($hDll, "char:cdecl", "sendChar", "char", 'A') ; <-- doesn't work
ConsoleWrite("@error: " & @error & " Returns: " & $return2[0] & @CRLF)

DllClose($hDll)

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/* To use this exported function of dll, include this header
 * in your project.
 */

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

#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

extern "C" __declspec(dllexport)

int Add(int a, int b )
{
    return a + b;
}

extern "C" __declspec(dllexport)

float AddF(float a , float b )
{
    return a + b;
}

extern "C" __declspec(dllexport)

char sendChar(char letter)
{
    return letter;
}
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

Try Asc('A') since char is technically a number.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

...

...
Global $return2 = DllCall($hDll, "char:cdecl", "sendChar", "char", 'A') ; <-- doesn't work

...

Is "char" a valid data type parameter for DllCall().

I was thinking "str", but I could be wrong.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

what is the return of @error? If @error = 2 you should try byte instead of char and use Asc('A') :mellow:

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Yeah, 'char' isn't supported by DLLCall unfortunately. The call would need 'byte' or 'ubyte' and then a simple conversion using BinaryToString().

*edit: Oops, a Chr() conversion would be better

Edited by Ascend4nt

Share this post


Link to post
Share on other sites

@Ascend4nt: Using ubyte is not a good idea. byte is already an unsigned type, so ubyte is unnecessary and can be removed at any time. I think, it already is.

Yes, for this purpose, the functions Asc and Chr are the best.

When you have wchar, you should use "WORD" or "ushort" and AscW/ChrW


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

I didn't realize char isn't supported. My bad.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hai guyz, thanks for the replies! Yes! It took me a minute to figure out what yall meant. Thank you.

Global $hDll = DllOpen("D:\CodeBlocks\C++ Programs\Line Alg DLL\bin\Debug\Line Alg DLL.dll")

Global $return = DllCall($hDll, "int:cdecl", "AddInt", "int", 6, "int", 4) ; <-- works

Report($return[0])

Global $return1 = DllCall($hDll, "float:cdecl", "AddFloat", "float", 5.5, "float", 4.5) ; <-- works

Report($return1[0])

Global $return2 = DllCall($hDll, "str:cdecl", "sendString", "str", "hello") ; <-- works

Report($return2[0])

Global $return3 = DllCall($hDll, "byte:cdecl", "sendChar", "byte", Asc('A')) ; <-- works

Report(Chr($return3[0]))

DllClose($hDll)

Func Report($report)
    Return ConsoleWrite("Return: " & $report & @CRLF)
EndFunc ;==>Report

"main.h"

//*************************************************
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/* To use this exported function of dll, include this header
 * in your project.
 */

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


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
//*************************************************

#include "main.h"
#include <Windows.h>

using namespace std;

extern "C" __declspec(dllexport)

int AddInt(int a, int b)
{
    return a + b;
}

extern "C" __declspec(dllexport)

float AddFloat(float a , float b)
{
    return a + b;
}

extern "C" __declspec(dllexport)

LPSTR sendString(LPSTR uhhhhh_like_Words_and_Stuff)
{
    return uhhhhh_like_Words_and_Stuff;
}

extern "C" __declspec(dllexport)

char sendChar(char uhhhhh_like_Words_and_Stuff)
{
    return uhhhhh_like_Words_and_Stuff;
}
Edited by jaberwocky6669

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