C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Re: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by jeeswg » 08 Feb 2019, 09:25

- Haha there aren't always easy answers for laying things out. I might use this, but people could criticise it also. I do think that Allman style is always helpful though.

Code: Select all

HWND winWait(FindWindow fw,
	sleep _sleep,
	HWND    hWndParent,
	HWND    hWndChildAfter,
	LPCWSTR lpszClass,
	LPCWSTR lpszWindow,
	unsigned int timeout,
	unsigned int sleep_time)
{
- Hmm, a lot of things to try re. the machine code. Perhaps checking small blocks of say 256 bytes (or higher) could be a way, and than narrowing down to smaller blocks etc. I'd have to do a lot of experimentation.
- I had wondered if instead of a function, a lookup array could make things faster (to 'convert' case).
- Btw is there a way to use fixed values/temporary variables, or must everything be function parameters? Cheers.

Re: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by Helgef » 08 Feb 2019, 08:45

- I was trying to achieve a case-insensitive 'StrCmp' get position function, in machine code, but it came out pretty slow, in case you had any ideas.
It is slow because you make 2 functions calls per iteration. You can call :arrow: _strup once per each string, or perhaps call it on substrings. You need to make copies then ofc.
jeeswg wrote:
08 Feb 2019, 08:16
the problem was the curious way you laid out your code
I am sorry for the inconvenice.

Cheers :beer:

Re: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by jeeswg » 08 Feb 2019, 08:16

- I fixed the code:

Code: Select all

;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

q:: ;test call MessageBox
pFuncMessageBox := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"user32", Ptr), AStr,"MessageBoxW", Ptr)
DllCall(pFuncMessageBox, Ptr,0, Str,"prompt", Str,"title", UInt,0)
pFunc := MCode("2,x86:g+wci0QkLIlEJAyLRCQoiUQkCItEJCSJRCQEi0QkIIkEJP9UJDCD7BCDxBzD,x64:SP9kJCg=")
Loop, 3
	DllCall(pFunc, Ptr,0, Str,"prompt " A_Index, Str,"title", UInt,0, Ptr,pFuncMessageBox, "Cdecl")
return

/*
#include <windef.h>

typedef int WINAPI (*MessageBoxW)(HWND,LPCWSTR,LPCWSTR,UINT);

void CallMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, MessageBoxW mx)
{
	//MessageBoxW(0, L"prompt", L"title", 0);
	mx(hWnd, lpText, lpCaption, uType);
	return;
}
*/
- I have never thrown something random into code, hoping it will work.
- Maybe you've done that occasionally, I don't know.
- As ever, I use manuals or existing code and use that as a basis.
- In this case, the problem was the curious way you laid out your code, here:

Code: Select all

HWND winWait(
	FindWindow fw,
	sleep _sleep,
	HWND    hWndParent,
	HWND    hWndChildAfter,
	LPCWSTR lpszClass,
	LPCWSTR lpszWindow,
	unsigned int timeout,
	unsigned int sleep_time){
- Btw why do you keep omitting parentheses and using single quotes when writing AHK v2 code. It's confusing, it doesn't look like any other programming language, it's less readable, and it makes it harder to port the code to AHK v1 (or any other programming language).
- (Also, if you don't use parentheses, you have to add them if you want to shift the code around cf. have the code at the start of the line.)
- (And single quotes can be confusing as they imply characters rather than strings.)

- It's quite interesting what you've done here with windef.h, and I thank you for it.
- I was trying to achieve a case-insensitive 'StrCmp' get position function, in machine code, but it came out pretty slow, in case you had any ideas. Thanks.
JavaScript vs AutoHotKey (Simple Speed Test) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=61602&p=262545#p262545

- [EDIT:] Another issue is using fixed values or temporary data in functions. Is it possible to define fixed values, or temporary variables, or must we pass everything as function parameters?

Re: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by Helgef » 08 Feb 2019, 07:54

;test call MessageBox (not working)
Still going with the random code testing approach... :yawn: .
You have to pass the function pointer void CallMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, MessageBoxW mx).

Re: C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by jeeswg » 08 Feb 2019, 07:40

- @Helgef: I've updated the title, things are now less offtopic than they were.
- Is it possible to fix this? Thanks.

Code: Select all

;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

q:: ;test call MessageBox (not working)
pFunc := MCode("2,x86:g+wci0QkLIlEJAyLRCQoiUQkCItEJCSJRCQEi0QkIIkEJDHA/9CD7BCDxBzD,x64:McBI/+A=")
Loop, 3
	DllCall(pFunc, Ptr,0, Str,"prompt " A_Index, Str,"title", UInt,0, "Cdecl")
return

/*
#include <windef.h>

typedef int WINAPI (*MessageBoxW)(HWND,LPCWSTR,LPCWSTR,UINT);

void CallMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
{
	MessageBoxW mx;
	//MessageBoxW(0, L"prompt", L"title", 0);
	mx (hWnd, lpText, lpCaption, uType);
	return;
}
*/

Re: C++: TDM-GCC: error with bool

Post by Helgef » 08 Feb 2019, 04:45

Almost nothing in this thread has anything to do with the original topic. This post is slightly on topic at the very end.
a limitation of C++ compared to AutoHotkey is that you cannot break out of nested loops by doing 'break n'.
You can use goto, which is more readable and maintainable if used properly.
using an HWND parameter, and calling a machine code function from another machine code function.
You can include windef.h if you want to use common Windows types. Two recent examples which comes to mind, :arrow: here and :arrow: here (which also uses stdbool, so this post is not completely off topic ;)).

Cheers.

Re: C++: TDM-GCC: error with bool

Post by jeeswg » 07 Feb 2019, 11:37

- So far when I have written machine code functions for AHK, they haven't called other functions.
- But here are some fundamental examples, if I did ever want to call other functions from a function.
- The code examples demonstrate: checking the bitness of the AHK script, using an HWND parameter, and calling a machine code function from another machine code function.

Code: Select all


;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

q:: ;test get bitness (for process that runs machine code)
pFunc := MCode("2,x86:uCAAAADD,x64:uEAAAADD")
MsgBox, % DllCall(pFunc, "Cdecl")
return

/*
int ThisProcessGetBitness()
{
#ifdef _WIN64
	return 64;
#else
	return 32;
#endif
}
*/

;==================================================

w:: ;test call MessageBox
pFuncMessageBox := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"user32", Ptr), AStr,"MessageBoxW", Ptr)
DllCall(pFuncMessageBox, Ptr,0, Str,"prompt", Str,"title", UInt,0)
pFunc := MCode("2,x86:g+wci0QkLIlEJAyLRCQoiUQkCItEJCSJRCQEi0QkIIkEJP9UJDCD7BCDxBzD,x64:SP9kJCg=")
Loop, 3
	DllCall(pFunc, Ptr,0, Str,"prompt " A_Index, Str,"title", UInt,0, Ptr,pFuncMessageBox, "Cdecl")
return

/*
void CallMessageBoxW(void* hWnd, short* lpText, short* lpCaption, unsigned int uType, int _stdcall(*MyFunction)(void*,short*,short*,unsigned int))
{
	//MessageBoxW(0, L"prompt", L"title", 0);
	(*MyFunction)(hWnd, lpText, lpCaption, uType);
	return;
}
*/

;==================================================

e:: ;test call towupper
vNum := 97 ;a
pFuncToWUpper := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"msvcrt", Ptr), AStr,"towupper", Ptr)
MsgBox, % DllCall(pFuncToWUpper, Int,vNum, "Cdecl") ;65 ;A
pFunc := MCode("2,x86:/2QkCA==,x64:SP/i")
MsgBox, % DllCall(pFunc, Int,vNum, Ptr,pFuncToWUpper, "Cdecl")
return

/*
void CallToWUpper(int wc, int _cdecl(*MyFunction)(int))
{
	//ToWUpper(97);
	(*MyFunction)(wc);
	return;
}
*/

;==================================================

r:: ;test call custom add function
pFuncAdd := MCode("2,x86:i0QkCANEJATD,x64:jQQRww==")
pFunc := MCode("2,x86:/2QkDA==,x64:Sf/g")
vNum1 := 1, vNum2 := 2
MsgBox, % DllCall(pFuncAdd, Int,vNum1, Int,vNum2, "Cdecl")
MsgBox, % DllCall(pFunc, Int,vNum1, Int,vNum2, Ptr,pFuncAdd, "Cdecl")
return

/*
int Add(int num1, int num2)
{
	return num1 + num2;
}

void CallAdd(int num1, int num2, int _cdecl(*MyFunction)(int,int))
{
	(*MyFunction)(num1, num2);
	return;
}
*/

;==================================================
- Examples of machine code that use pointers to other functions:
buf/byteWrite.c at master · HelgeffegleH/buf · GitHub
https://github.com/HelgeffegleH/buf/blob/master/c%20source/byteWrite.c
'To call our function in MCode we have to give it the pointer to our function.'
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://www.autohotkey.com/boards/viewtopic.php?f=7&t=32

- [EDIT:] A further example:
[compare strings case insensitive, find first difference]
JavaScript vs AutoHotKey (Simple Speed Test) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=61602&p=262545#p262545

- @nnnik: Interesting.

Re: C++: TDM-GCC: error with bool

Post by nnnik » 07 Feb 2019, 10:20

void* seems like a good choice for hWND - after all it's just a pointer sized thing instead of an actual pointer to something.
https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
^This includes the actual data definitions.
I don't have any decent examples. My latest MCode project was about creating a Machine Code boosted Matrix calculation library.
For that I even wrote a new MCode compiler that should solve a few issues and even allow us to handle Floats in the first place.
However after I got really into the details I discovered that the prototype still couldn't deal with a few issues - easily tripling the work necesaary until I get decent results.
The first tests also shown me that the code starts throwing 0xc000005 errors left and right after a while.
I still haven't found the reasons as to why this is the case. Without proper debugging tools for MCode people will always be lost and take a long time to write MCode.
This blew the project out of proportion and I don't feel like touching it ever again.

Re: C++: TDM-GCC: error with bool

Post by jeeswg » 07 Feb 2019, 10:07

- The #if stuff was one of the things I was thinking of. However, I couldn't find any examples.
- Do you have any good examples of machine code in AHK handling pointers (that aren't pointers to strings) or #if stuff? Or for machine code (full scripts) in AHK generally?
- (It seems odd reinventing the wheel for fundamental things.)
- (I'll test it myself but) out of interest can char* or short* be used for an HWND? Thanks.

Re: C++: TDM-GCC: error with bool

Post by nnnik » 07 Feb 2019, 09:13

Pointers are pointer types and not integers.
You need to use char* or void* or similar.
If you want to use different types depending on the bitness the #if stuff could be used to define an integer type that changes size depending on the bitness of the target architecture.

Re: C++: TDM-GCC: error with bool

Post by jeeswg » 07 Feb 2019, 07:47

- According to this list:
The GNU C Reference Manual
https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Data-Types
- The following integer types available:
unsigned char
char
short int
unsigned short int
int
unsigned int
long int
unsigned long int
long long int
unsigned long long int
- Also available is:
_Bool
- But what about pointer-sized integers, e.g. how do we handle HWND? Thanks.

Re: C++: TDM-GCC: error with bool

Post by jeeswg » 17 Dec 2018, 14:56

- Also, a limitation of C++ compared to AutoHotkey is that you cannot break out of nested loops by doing 'break n'. So instead, I do the equivalent of 'if var then break' within each loop. (I often like to code in AutoHotkey first and then convert to C++.)
- Also again, min/max are not built-in, so I use the ternary operator.
C++: TDM-GCC: minimising includes - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 23&t=59986
- And one must be careful to use == (AHK and C++: comparison) for comparison instead of = (AHK: comparison, C++: assignment).

Re: C++: TDM-GCC: error with bool

Post by nnnik » 14 Dec 2018, 14:12

Gcc and VC++ are generally incompatible.
You have to supply different sources using #IF.
Generally I would just drop support for one and choose one to use.
(There are a lot of incompatibilities for example the way you compile against libraries - vs uses some #pragma stuff with some special instructions and gcc expects yu to pass them to the command line)

C++: TDM-GCC: error with bool (and other VC++/AHK to TDM-GCC issues)

Post by jeeswg » 14 Dec 2018, 13:59

- In relation to:
MCode4GCC -- C/C++ to MCode Generator - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=4642

- I received an error message when trying to compile C++ code that used the 'bool' type:
C:\Users\me\Desktop\z gcc temp source.c: In function 'inbufstr':
C:\Users\me\Desktop\z gcc temp source.c:5:2: error: unknown type name 'bool'
- I was able to compile if I used '_Bool' instead, however, the code wouldn't run in Visual Studio when using '_Bool'.
- Is there a two-way compatible solution?
- Would it be worth just using char instead, what would be a good alternative that didn't decrease performance, and that wouldn't require any script rewrites (other than changing 'bool').

- When I used '_Bool' the machine code appeared to work, although I got this error, which may or may not be related to types:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
- I pity the bool. Like a bool in a china shop. (C Bool by Paul McCartney.) Thanks for reading.

Top