Debugging Unmanaged C++ Dll

K

krupa.p

Hi All,

I have created an unmanaged C++ dll project with some exported
functions in it. I am trying to call these functions from my VS 2005 C#
project for a Windows CE device using PInvoke. This seems to be working
fine except that the values returned by the functions (in unmanaged
Dll) are not proper.

Is there a way I can debug these functions in the unmanaged code? I
tried to create and write to a text file in these functions, but the
file is not created on the device at all. Can anyone tell me of a way I
can capture values returned by the function in the unmanged code?

Here is the code I used in the unmanaged function to write to a file.

-------------------------------------------------------------------------------------------------------------------------------

BRIGHTNESSCONTROL_API DWORD GetPowerStatus(LPBYTE power_status, LPDWORD
batt_life_time, LPBYTE batt_life_percent)
{
DWORD dwLen;
SYSTEM_POWER_STATUS_EX2 sps;

//file handle
HANDLE hFile;
//something to contain the number of bytes read
DWORD dwNumWritten;
//a boolean test variable, to test for success of reads
BOOL bTest;
//a buffer... can actually be of any type
DWORD dwBuffer;

dwLen = GetSystemPowerStatusEx2(&sps, sizeof(sps), TRUE);

*power_status = sps.ACLineStatus;
*batt_life_time = sps.BackupBatteryLifeTime;
*batt_life_percent = sps.BatteryLifePercent;

const WCHAR fp = (WCHAR) "\Storage Card\volume.txt";
//filepath = (WCHAR) "\storage card\volume.txt";
LPCWSTR filepath = &fp;
//*filepath = fp;
//dwBuffer = *pdwVolume;
hFile = CreateFile(filepath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

if(hFile != INVALID_HANDLE_VALUE)
{
//SetFilePointer(hFile, 0, 0, FILE_END);
bTest= WriteFile(hFile, batt_life_time, sizeof(batt_life_time),
&dwNumWritten,NULL);
bTest= CloseHandle(hFile);
}


return dwLen;

}
 
D

Doug Harrison [MVP]

Hi All,

I have created an unmanaged C++ dll project with some exported
functions in it. I am trying to call these functions from my VS 2005 C#
project for a Windows CE device using PInvoke. This seems to be working
fine except that the values returned by the functions (in unmanaged
Dll) are not proper.

Is there a way I can debug these functions in the unmanaged code? I
tried to create and write to a text file in these functions, but the
file is not created on the device at all. Can anyone tell me of a way I
can capture values returned by the function in the unmanged code?

Here is the code I used in the unmanaged function to write to a file.

-------------------------------------------------------------------------------------------------------------------------------

BRIGHTNESSCONTROL_API DWORD GetPowerStatus(LPBYTE power_status, LPDWORD
batt_life_time, LPBYTE batt_life_percent)
{
DWORD dwLen;
SYSTEM_POWER_STATUS_EX2 sps;

//file handle
HANDLE hFile;
//something to contain the number of bytes read
DWORD dwNumWritten;
//a boolean test variable, to test for success of reads
BOOL bTest;
//a buffer... can actually be of any type
DWORD dwBuffer;

dwLen = GetSystemPowerStatusEx2(&sps, sizeof(sps), TRUE);

*power_status = sps.ACLineStatus;
*batt_life_time = sps.BackupBatteryLifeTime;
*batt_life_percent = sps.BatteryLifePercent;

const WCHAR fp = (WCHAR) "\Storage Card\volume.txt";

That should be:

const WCHAR* const fp = L"\\Storage Card\\volume.txt";

Note the doubled-up backslashes, the use of the 'L' prefix to make it a
wide string literal, and the pointer declarator (the star). I also made fp
itself const, so you can't change it later. (It looks like you won't need
to.)
//filepath = (WCHAR) "\storage card\volume.txt";
LPCWSTR filepath = &fp;
//*filepath = fp;

Get rid of filepath; its purpose is served by fp. It looks like you tried
to cast things into correctness. That rarely if ever works.
//dwBuffer = *pdwVolume;
hFile = CreateFile(filepath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

if(hFile != INVALID_HANDLE_VALUE)
{
//SetFilePointer(hFile, 0, 0, FILE_END);
bTest= WriteFile(hFile, batt_life_time, sizeof(batt_life_time),
&dwNumWritten,NULL);
bTest= CloseHandle(hFile);
}


return dwLen;

}

Besides writing to a file, see if Windows CE supports OutputDebugString.
Then see if Sysinternals DebugView works.
 
K

krupa.p

Thanks a lot Doug! It worked. The file is created on the device.
However, the values written are some weird characters. I guess it is
printing the ascii equivalent. Could you tell me how to print the
values in a readable format?

Regards,
Krupa
 
D

Doug Harrison [MVP]

Thanks a lot Doug! It worked. The file is created on the device.
However, the values written are some weird characters. I guess it is
printing the ascii equivalent. Could you tell me how to print the
values in a readable format?

I suppose you want to convert the numeric values to text. If you were using
C++ streams, you'd say things like:

strm << i; // i is an int

For C and C++, there's fprintf:

fprintf(fp, "%d", i);

For the raw Windows API, there's wsprintf (or sprintf for a standard
function), which writes formatted text into a buffer, which you can then
write to your file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top