In part 1, I had written about how mixed-mode code can be written using the ‘inline’ approach. In this post I will write about the second approach.

SUA Application that links to a custom Win32 dll which exposes functions and data whose datatypes are consistent with that of SUA datatypes:

This paradigm, though technically not very different from the first paradigm discussed, solves one of the drawbacks of the first approach – namely ‘header conflicts’.  This paradigm segregates the scope of data types of SUA and Windows, thereby removing any possibility of conflict.  The onus is on the programmer to take care of the mapping between SUA and Windows types.

Below is the list of steps that I would follow to write code using this approach.

1.       Identify the  Win32 function that needs to used

2.       Build a custom win32 dll with a wrapper function that calls the Win32 function that was indentified in step 1.

Note: The wrapper function should not return data which is of a Windows type. It should rather return data that is of SUA datatype. ( or make sure the SUA part of the code can understand the Windows type)

For example, consider CloseWIndow() win32 function. The syntax of this function as defined by msdn is BOOL CloseWindow(HWND hWnd).

 

To use this in the SUA/POSIX Code, build a custom win 32 dll that exports a wrapper function as below

 

__declspec(dllexport) char myCloseWindow(int hWnd) {

       return CloseWindow(hWnd);

}

 

The wrapper function, myCloseWindow, takes int (which is of size same as HWND) as argument and returns char (which is of same size as BOOL). HWND and BOOL are windows types and are not defined by SUA headers.

 

 

3.       Call the exported wrapped function from SUA/POSIX code. The SUA/POSIX code will have to link the custom Win32 dll in order to call the exported function.

Ok. So, when should one favor this approach over the first one.

1.       When there are header conflicts

2.       When there is clear separation between functionality the SUA/POSIX code and functionality of win32 call.  Example: SUA/POSIX code that calls Win32 API’s to display Graphical user interface (GUI).

The post about win32 gui and posix code is a good example of code written using this approach.