Why doesn't GCC compile this code?
c++
Solution
This code RELIES on the Microsoft specific functionality of `__try` and `__except`. There is no other way to solve this, as doing an `IN` instruction from a user-mode process in Windows will normally fail. In VMWare, any such operation is intercepted by the VMWare VMM (Virtual Machine Monitor, sometimes also called Hypervisor). It then checks the special values in some registers and "accepts" the instruction with a new value in `ebx` to indicate that "Yes, I'm a VMWare system". If you run this on a plain old Windows system, it will get an General Protection Fault for trying to use a IO-port that isn't available to the application [in general, IO ports aren't availbe to the user-mode application code].
You need to compile this using a Microsoft compiler (and with the suitable "enable exception handling" flag set - sorry, can't remember what that flag is, but it will tell you if it's not on that it's wrong). GCC doesn't support this feature.
However, you can test for VMWare using the CPUID instruction instead of using `IN`. This will work much better in a GCC compiler, as it will not fault the processor if there is no VM present [For the pedants: As long as the processor is later than some version of 486 from the 1990's]. Instead, it will just not give the right values in the registers (no change to the register values). You may need to write a `cpuid` inline assembler function, but that shouldn't be too difficult to google up.
See this KB-article on VMwares site: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
Alternatively, if you really insist on using Windows Structured exception handling, you can do it yourself:
push handler // Save address of your handler.
push fs:[0] // Save address of any previous handler
mov fs:[0], esp // Save pointer to current exception record.
... // all the stuff setting registers, doing IN, etc.
jmp cleanup
handler:
mov [rc], 0 // Set "rc= false".
cleanup:
pop fs:[0]
add esp, 4 // Remove handler
(Note, I just wrote that from a bit of googling - it's a LONG time since I used anything like that on Windows, so it may not work)
Problem
I am a Java, Python programmer trying to fiddle with C++. I have following code snippet - ``` #include <windows.h> bool IsInsideVMWare() { bool rc = true; __try { __asm { push edx push ecx push ebx mov eax, 'VMXh' mov ebx, 0 // any value but not the MAGIC VALUE mov ecx, 10 // get VMWare version mov edx, 'VX' // port number in eax, dx // read port // on return EAX returns the VERSION cmp ebx, 'VMXh' // is it a reply from VMWare? setz [rc] // set return value pop ebx pop ecx pop edx } } __except(EXCEPTION_EXECUTE_HANDLER) { rc = false; } return rc; } ``` I am getting following compile error - `__try' undeclared (first use this function) `__except' undeclared (first use this function) What does that vague message even mean? Because try is undeclared I should try to use it first?? Edit: IDE - `Codeblocks` Compiler - `GCC`