Jump to content
Sign in to follow this  
piccaso

Using C++ Compiler to help with DllCall() and related

Recommended Posts

I was tired of searching and translating headers to find the right DllCall/DllStruct type to use so i

made 2 Macros to help with that:

t(<Type>)
Displays Size and Primitive type

Type can be a Type, Constant or an existing variable.

c(<Constant>)
Translates constants into AutoIt Code

Tested with Gnu c++ 3.x, Borland C++ 5.5.1, MSVC8

Here is the source with some examples:

// Headers reqired by Macros
#include <typeinfo> // typeid
#include <cstdio> // printf
#ifdef __GNUG__
#include <cstdlib> // free
#include <cstring> // strlen
#include <cxxabi.h> // abi::__cxa_demangle
#endif // __GNUG__

// Macros
#define c(c) std::printf("Global Const $%s = 0x%X\n",#c,c);
#define _s(s) std::printf("sizeof(%s) = %d (0x%X) Bytes, %d Bit",#s,sizeof(s),sizeof(s),sizeof(s)*8)
#ifdef __GNUG__ // Workaroud for crappy g++ typeid(T).name()
#define t(t) _s(t);__demangle(typeid(typeof(t)*).name(),#t);
void __demangle(const char * type, char*s){
  int st; size_t n; char* unm = abi::__cxa_demangle(type,NULL,0,&st);
  if (unm != NULL){
      n = strlen(unm);unm[n-1]=0;
      printf(", Primitive: %s\n",unm);
      free(unm);
  } else printf("\ntype: %s error: %d unmangled: %s\n",s,st,type);
}
#else // __GNUG__
#define t(t) _s(t);std::printf(", Primitive: %s\n",typeid(t).name());
#endif // NOT __GNUG__


///////////////////////
// Include headers here
///////////////////////
#include <windows.h>

int main (){
    //////////////////
    // Use Macros Here
    //////////////////
     
    t(size_t);
    t(LPDWORD);
    t(HRESULT);
    t(LRESULT);
    t(ATOM);
    t(HGDIOBJ);
    t(LPWSTR);
    
    // Display primitive of constants
    t(STD_INPUT_HANDLE);
    t(INVALID_HANDLE_VALUE);
    t(TIME_ZONE_ID_INVALID);
    
    // Display primitive of variables
    PFLOAT pf;
    t(pf);
    
    // Translating Constants
    c(STATUS_WAIT_0);
    c(STATUS_ABANDONED_WAIT_0)
    c(STATUS_USER_APC);
    c(STATUS_TIMEOUT);
    c(STATUS_PENDING);
    
    return 0;
}

This does not only work with windows header, and its pretty usefull if you are translating from headers like libcurl which some macro maniac wrote.

Most of the windows types are easily identified by their naming but still some of them might be confusing.

For example: I would think that 'HRESULT' is a handle and use 'ptr' or 'hwnd' which might work but in order to be correct it should be 'long'.

If someone of you has similar tricks or improvement ideas on this one please post them :)

Edited by piccaso

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites

If this is a C++ application (g++ means it is), then you need to fix this:

#include <stdio.h> // printf
#include <string.h> // strlen
#include <stdlib.h> // malloc,free

Those are C libraries. They need to be cstdio, cstring and cstdlib respectively.

The file cxxabi.h is not a standard file. That's the only reason it won't compile (assuming you fix the above mentioned header issue).

And you forgot "return 0" from main().

Share this post


Link to post
Share on other sites

Thank you Valik.

I got it to work on other compilers too, but for some reason borlands still requires stdio.h to be included.

Probably my mistake, i'm more familiar with C, not much into that '++' :)

The only reason for using C++ was to get the typeof(T).name() functionality...


CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites

Valery, have you ever looked at the headers? I wouldn't trust an automated tool to the task. In general, it's a pain in the ass to figure out what a type really is. It's usually #defined, typedef'd and composed of other #defined/typedef'd types so you sometimes have to drill down really deep to figure out what native types are actually being used.

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  

×
×
  • Create New...