Thursday, December 18, 2008

Pass complex data types to 3rd party DLLs in Axapta

If you are using third-party dll files, you may encounter a problem that you need pass a complex data types to or from the dlls, for example, struct, array etc.  
There is an example to show you how to pass struct data type to dlls. 
Below is the api of a given dll. 
/* C++ API – SampleDll.dll
typedef struct
{
LPCTSTR lpszFontName; 
int nCharacterExtra; 
int nFontHigh; 
int nSpacing; 
int nAlign; 
LPCTSTR lpszTextString; 
} BCTEXTINFO, *LPBCTEXTINFO;

BCENCODE_API DWORD WINAPI SampleDllMethod(LPBCTEXTINFO lpTextInfo);
*/
 
And we can use Binary class to store the string's address instead of including the string directly in the struct. Here is a sample code to show you how to pass a struct to dll.
    client static int SampleMethod(str _name, int _chExtra, int _fontHeight, int _spacing, int _align, str _content)
    {
        int ret;
        Binary lpTextInfo  = new Binary(24);  // 6 * 4 = 24
        Binary biName     = new Binary(strlen(_name) + 1);
        Binary biContent  = new Binary(strLen(_content) + 1);
 
        DLL _winApiDLL  = new DLL('SampleDll.dll');
        DLLFunction _SampleMethod = new DLLFunction(_winApiDLL,'SampleDllMethod');
        ;
 
        _ SampleMethod.returns(ExtTypes:: DWord);
        _ SampleMethod.arg( ExtTypes::Pointer, ExtTypes::Pointer);
        biName.string(0, _name);
        biContent.string(0, _content);
 
        lpTopTextInfo.binary(0, biName);
        lpTopTextInfo.dWord(4, _chExtra);
        lpTopTextInfo.dWord(8, _ fontHeight);
        lpTopTextInfo.dWord(12, _ spacing);
        lpTopTextInfo.dWord(16, _ align);
        lpTopTextInfo.binary(20, biContent);
 
        ret = _ SampleMethod.call(lpTextInfo);
        return ret;
    }

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...