John
John

Reputation: 11

MATLAB wchar_t Equivalent Type

The C-Code is stored in a DLL. I can load the DLL in MATLAB with the loadlibrary function. I am having trouble passing the wchar_t*[] parameter to the function. I do not know how to create this data type in MATLAB. Does anyone know how to create this type to pass to the calllib function?

MATLAB Code:

loadlibrary('test.dll', 'test.h');

str = '0';
ptr = libpoiner('voidPtrPtr', [int8(str) 0])

calllib('test.dll', 'testFunction', ptr) %this parameter does not match the wchar*[] type

outVal = ptr.Value

C-Code:

void testFunction(wchar_t* str[])
{
    str[0] = L"test";
}

Output:

MATLAB allows the function to complete. The outVal variable is filled with garbage values.

Upvotes: 1

Views: 412

Answers (2)

John
John

Reputation: 11

I figured it out. I changed the MATLAB code to the following:

loadlibrary('test.dll', 'test.h');

str = '0';
ptr = libpoiner('voidPtrPtr', [uint16(str) 0])

calllib('test.dll', 'testFunction', ptr) %this parameter does not match the wchar*[] type

outVal = ptr.Value

expectedOutput = char(outVal); %convert to ASCII

It outputs the values in decimal which confused me. When I converted them to ASCII, everything made sense.

Upvotes: 0

Juderb
Juderb

Reputation: 735

If you are able to modify the C header files, you may try the following:

  1. Adjust the header file to convert all wchar_t * to unsigned short *.

  2. On the MATLAB side, the corresponding type would then be a uint16 array.

  3. You could then typecast the uint16 array to char.

Upvotes: 0

Related Questions