About Monkey 2 › Forums › Monkey 2 Programming Help › Struggling with Struct arrays
Tagged: api, compiler, Extern, field arrays, internal error, Struct, win32
This topic contains 6 replies, has 2 voices, and was last updated by
DruggedBunny
1 year, 3 months ago.
-
AuthorPosts
-
January 9, 2018 at 1:01 am #12762
Struct arrays are weird! Having to New them in the struct itself seems unintuitive (seems like more of a runtime thing), but although this seems to parse fine, it doesn’t run (“Failed to start process blah.exe”):
Monkey123456789101112131415161718192021222324252627282930#Import "<std>"#Import "<windows.h>"#Import "<tlhelp32.h>"Using std..Const MAX_PATH:IntStruct PROCESSENTRY32FIeld dwSize:UintFIeld cntUsage:UintFIeld th32ProcessID:UintField th32DefaultHeapID:ULong PtrFIeld th32ModuleID:UintFIeld cntThreads:UintFIeld th32ParentProcessID:UIntField pcPriClassBase:LongFIeld dwFlags:UIntField szExeFile:Short[] = New Short [MAX_PATH]EndFunction Main ()Local proc:PROCESSENTRY32 = New PROCESSENTRY32proc.dwSize = libc.sizeof (PROCESSENTRY32)End(Tried hard-coding the value of MAX_PATH here — surprised that builds.)
This was just me trying to figure out the Struct array syntax, but once I stick it into an Extern block, it fails to even parse, with “Error : Expecting string literal” on this line:
Monkey1Field szExeFile:Short[] = New Short[MAX_PATH]My understanding is that just declaring the const in an Extern block will pick up the value from the .h imports, is that right?
Monkey12345678910111213141516171819202122232425262728293031323334#Import "<std>"#Import "<windows.h>"#Import "<tlhelp32.h>"Using std..ExternConst MAX_PATH:IntStruct PROCESSENTRY32FIeld dwSize:UintFIeld cntUsage:UintFIeld th32ProcessID:UintField th32DefaultHeapID:ULong PtrFIeld th32ModuleID:UintFIeld cntThreads:UintFIeld th32ParentProcessID:UIntField pcPriClassBase:LongFIeld dwFlags:UIntField szExeFile:Short[] = New Short[MAX_PATH]EndPublicFunction Main ()Local proc:PROCESSENTRY32 = New PROCESSENTRY32proc.dwSize = libc.sizeof (PROCESSENTRY32)End(Besides the syntax/apparent parse/run errors, do I need to declare an external struct like this, ie. am I going about this the right way?)
January 9, 2018 at 1:02 am #12763January 9, 2018 at 4:55 am #12766You can’t use mx2 arrays in an extern like that, because an mx2 array is NOT the same as a C array.
The easiest way to handle C arrays is to use a Ptr instead of an array (in C, arrays ARE pointers), eg: change ‘int[32]’ to ‘Int Ptr’. You’ll still be able to access it via [] etc, but be aware that the compiler wont be able to sanity check static array sizes.
As per the excellent unicode-for-all doc, I would suggest explicitly using Struct PROCESSENTRY32W, ie: the wide char version where TCHAR is ushort, something like…
Monkey123456789101112131415ExternStruct PROCESSENTRY32WField dwSize:UIntField cntUsage:UInt...Field szExeFile:UShort PtrEndFunction Main()Local proc:PROCESSENTRY32Wproc.dwSize=libs.sizeof(proc)...EndYou don’t need to ‘New’ structs although it doesn’t really hurt.
January 9, 2018 at 8:07 am #12772OK, will give that a shot later. Didn’t even realise there were W versions of Win32 structs!
January 9, 2018 at 7:41 pm #12783This revised version gives me “Translator.Trans() Internal Error”:
Monkey1234567891011121314151617181920212223242526272829303132333435#Import "<std>"#Import "<windows.h>"#Import "<tlhelp32.h>"Using std..ExternConst MAX_PATH:IntStruct PROCESSENTRY32WField dwSize:UIntField cntUsage:UIntField th32ProcessID:UIntField th32DefaultHeapID:ULong PtrField th32ModuleID:UIntField cntThreads:UIntField th32ParentProcessID:UIntField pcPriClassBase:LongField dwFlags:UIntField szExeFile:UShort PtrEndPublicFunction Main ()Local proc:PROCESSENTRY32W = New PROCESSENTRY32Wproc.dwSize = libc.sizeof (PROCESSENTRY32W)EndThis says…
Monkey1234567891011121314Mx2cc version 1.1.09***** Making app 'I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.monkey2' (windows release x86 gcc) *****Parsing...Semanting...Translating...? [?] : Error : Translator.Trans() Internal Error***** Uncaught Monkey 2 Throwable *****Done.Turns out that’s the error the first version gave, it was just that the output was hidden.
I get various different internal errors depending on what I do in Main with that last code…
This builds OK, but obviously does nothing:
Monkey1234Function Main ()Local proc:PROCESSENTRY32WEndNew’ing produces a different error:
Monkey1234Function Main ()Local proc:PROCESSENTRY32W = New PROCESSENTRY32WEndOutput for this is:
Monkey1234567891011121314151617181920212223242526Mx2cc version 1.1.09***** Making app 'I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.monkey2' (windows release x86 gcc) *****Parsing...Semanting...Translating...Compiling...Build error: System command failed:g++ -c -std=c++11 -D_WIN32_WINNT=0x0603 -m32 -O3 -DNDEBUG -I"I:/SpiderOak/DevTools/Monkey2/mx2builder/monkey2/modules/" -I"I:/SpiderOak/DevTools/Monkey2/mx2builder/monkey2/modules/monkey/native" -I"I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/" -DBB_NEWREFLECTION -I"I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.buildv1.1.09/windows_release/build/" -o "I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.buildv1.1.09/windows_release/build/win32_04proc32_0win32_04proc32.cpp.o" "I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.buildv1.1.09/windows_release/src/win32_4proc32_win32_4proc32.cpp"I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.buildv1.1.09/windows_release/src/win32_4proc32_win32_4proc32.cpp: In function 'void bbMain()':I:/SpiderOak/DevTools/Monkey2/monkey2-develop/modules/mojo3d/tests/win32 proc32.buildv1.1.09/windows_release/src/win32_4proc32_win32_4proc32.cpp:13:52: error: cannot convert 'bbNullCtor_t' to 'DWORD {aka long unsigned int}' in initializationPROCESSENTRY32W l_proc=PROCESSENTRY32W{bbNullCtor};^***** Fatal mx2cc error *****Internal mx2cc build error… and adding the sizeof line gives the “Translator.Trans() Internal Error”:
Monkey12345Function Main ()Local proc:PROCESSENTRY32W = New PROCESSENTRY32Wproc.dwSize = libc.sizeof (PROCESSENTRY32W)EndTried without New as well, but assume that is needed… same Trans() error anyway…
Monkey12345Function Main ()Local proc:PROCESSENTRY32Wproc.dwSize = libc.sizeof (PROCESSENTRY32W)EndJanuary 10, 2018 at 12:43 am #12788The argument for sizeof should be a value not a type, eg: libc.sizeof( proc ), but that error isn’t exactly helpful, will fix.
January 10, 2018 at 12:50 am #12789Cool, I had to not-New it, too (suspected so), but it’s working now with:
Monkey123456Function Main ()Local proc:PROCESSENTRY32Wproc.dwSize = libc.sizeof (proc)Print proc.dwSizeEndThanks!
-
AuthorPosts
You must be logged in to reply to this topic.