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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: