Unicode in DLL

forum.td.development (2005-2010)
Widmann Andreas

Unicode in DLL

Post by Widmann Andreas » 30 Oct 2009, 23:37

 Posted by:  Widmann Andreas 

Hi,

I have a problem to convert and understand the new unicode. We have a
wrapper-dll and in this dll there is a function GetWindowsDirectory.

Code
PDEWindowsDirectory function:
__declspec(dllexport) UINT WINAPI PDEGetWindowsDirectory( LPHSTRING
lpDir, UINT uSize )
{
UINT uReturn;
TCHAR cDir[4096];

if (uSize == 0)
uSize = 4095;

uReturn = GetWindowsDirectory( cDir, uSize );

if (!MakeLphstring( lpDir, cDir ))
uReturn = 0;

return uReturn;
}

The MakeLphstring function:

BOOL MakeLphstring( LPHSTRING lphString, LPCWSTR lpstrString )
{
LONG lLength;
int iLength;
TCHAR *lpstrCopy;
HSTRING hString;

iLength = lstrlen( lpstrString ) + 1;
if (!SWinInitLPHSTRINGParam( lphString, iLength ))
return (FALSE);

lpstrCopy = SWinHStringLock( *lphString, &lLength );

StringCchCopy( lpstrCopy, lLength, lpstrString );

SWinHStringUnlock( *lphString );

return (TRUE);
}

You can see that we have used the new LPCWSTR Parameter and the dll is
compiled with unicode method.

Now We have written a sample TD5.2 program:

External functions:
Library name: kernel32.dll
ThreadSafe: No
Function: GetWindowsDirectoryW
Description:
Export Ordinal: 0
Returns
Number: UINT
Parameters
Receive String: LPWSTR
Number: UINT

Test-Function:

Function: GetWinDir
Description:
Returns
Parameters
Static Variables
Local variables
String: l_sDir
String: l_sDir1
String: l_sDir2
Number: l_nLen
Number: l_nLen1
Number: l_nLenStr
Number: l_nLenStr1
Actions
Call SalSetBufferLength( l_sDir, 4096 )
Set l_nLenStr = GetWindowsDirectoryW( l_sDir, 4096 )

Set l_nLenStr = SalGetBufferLength( l_sDir )
If SalStrRightX( l_sDir, 1 ) != "\\"
Set l_sDir = l_sDir || "\\"
Set l_nLen1 = PDEGetWindowsDirectory( l_sDir1, 0 )
Set l_nLenStr1 = SalGetBufferLength( l_sDir1 )
If SalStrRightX( l_sDir1, 1 ) != "\\"
Set l_sDir1 = l_sDir1 || "\\"
Set l_nLenStr = SalStrLength( l_sDir1 )
Call SalMessageBox( l_sDir, "Return: " || SalNumberToStrX( l_nLen, 0 ),
MB_Ok )

The first call calls direct the kernel32.dll function and the returned
length is 22 and l_sDir = "C:\Windows" and also the concat is ok.
The second call calls the wrapper-dll and the returned length is 10 and
l_sDir1 = "C:\Windows" but the concat destroy the l_sDir1 string.

What is wrong? :?:

Thx for your help.

Andreas Widmann

Widmann Andreas

Unicode in DLL

Post by Widmann Andreas » 31 Oct 2009, 16:38

 Posted by:  Widmann Andreas 

Hi,

Now I have made a sample where you can see what the dll-call is doing.

Thank you for your help.

Andreas

Attachment: pdetest.zip

Widmann Andreas

Unicode in DLL

Post by Widmann Andreas » 01 Nov 2009, 10:27

 Posted by:  Widmann Andreas 

Hi,

for all who read this:

I found the error - you must multiply the string-length in MakeLphstring!!!

BOOL MakeLphstring( LPHSTRING lphString, LPCWSTR lpstrString )
{
LONG lLength;
int iLength;
TCHAR *lpstrCopy;
HSTRING hString;

iLength = (lstrlen( lpstrString ) + 1) * 2;
if (!SWinInitLPHSTRINGParam( lphString, iLength ))
return (FALSE);

lpstrCopy = SWinHStringLock( *lphString, &lLength );

StringCchCopy( lpstrCopy, lLength, lpstrString );

SWinHStringUnlock( *lphString );

return (TRUE);
}

Thomas Lauzi

Unicode in DLL

Post by Thomas Lauzi » 02 Nov 2009, 16:53

 Posted by:  Thomas Lauzi 

Hi,

let me ask you what you want to do?
->When you just want the windows directory, why do you make this...

Set sWinDir = VisDosGetEnvString('WINDIR')
Set sTempDir = VisDosGetEnvString('TEMP')

Regards,
Thomas L.

Widmann Andreas

Unicode in DLL

Post by Widmann Andreas » 05 Nov 2009, 18:15

 Posted by:  Widmann Andreas 

Hi,

this was only a sample. I know, that I can you your solution or to call
GetWindowsDir directly. The problem was the LPHSTRING in TD5.2. I forgot
to take the double length of the string.

Regards,
Andreas Widmann

Return to “td.development”

Who is online

Users browsing this forum: [Ccbot], SSRJMC and 0 guests