Skip to main content
Email Password   helpLost your password?

Introduction

Do you need to manage the interface between native and managed code? Using a few simple documentation comments and this tool, you can create up-to-date documentation of your native C/C++ code, complete with C# DllImport statements. This tool also auto-translates between C/C++ data types and C# data types.

Using the Code

This tool (DllImportDoc.exe) should be run with the current directory containing the *.C, *.CPP and *.H files that contain your autodoc comments. The generated documentation is written to DOCUMENTATION.HTML. There are three optional text files that you can also create in the same directory:

The autodoc comments to use in your C/C++ comments for your functions are:

Here is an example included in the SAMPLE.CPP file included in the source ZIP:

/*
@dllname:    CoolNativeCode.dll
*/

WCHAR    *wcTestString = "Test String!";

/****************************************************************************
@func:    GetStringLength | Determines number of characters in the string
@return:     uint | The number of characters
@note:    A note can go here.
@see:    GetStringValue
@area:    Strings
***************************************************************************/
DLLEXPORT UINT GetStringLength()
{
    return wcslen( wcTestString ) + 1;
}

/***************************************************************************
@func:    GetStringValue | Retreives the string value.
@param:   WCHAR* wcString | String buffer to receive the string
@param:   UINT uiCharactersAllocated | Number of characters wcString can hold
@return:  uint | number of characters copied to the string, 0 if failure
@note:    If the buffer is not large enough, then 0 is returned
@see:     GetStringLength
@area:    Strings
***************************************************************************/
DLLEXPORT UINT GetStringValue( WCHAR *wcString, UINT uiCharactersAllocated )
{
    uint uiLength = wcslen( wcTestString ) + 1;
    if ( uiLength > uiCharactersAllocated )
        return 0;
    StringCchCopy( wcString, uiCharactersAllocated, wcTestString );
    return uiLength;
}

It is also helpful to track the value and meanings of messages that can be sent from native code to managed windows. The autodoc comments to use in your C/C++ comments for your message #defines are:

Here is an example included in the SAMPLE.H file included in the source ZIP:

/*
@messheader: My Messages | 
    These are messages that can be sent from the native 
    code to a Window created by managed code.

The C# enum is

public enum ShellMessage
{
    DoSomething = 0x00,
    DoSomethingElse
}
*/

/*
@mess:    DO_SOMETHING | 0x00 | Cool message that indicates X.
@note:    Notes can go here
*/
#define    DO_SOMETHING        0

/*
@mess:    DO_SOMETHING_ELSE| 0x01 | Message indicating that Y has happened
*/
#define    DO_SOMETHING_ELSE        1

Points of Interest

The conversion between C/C++ data types and C# data types primarily takes place in PARAMETERS.CS. By default, LPGUID will map to ref Guid, HWND will map to IntPtr and anything that ends with * (int*) will map to an associated data type that starts with ref (ref int). If you would like to customize this mapping, simply update the case statement in ToManagedCode().

History

You must Sign In to use this message board.
 
 
Per page   
  
-- There are no messages in this forum --


Last Updated 13 Sep 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009