Hi,

I have created a dll which permit to get two informations, the flag saying
if the PDA is in charge and the flag which is giving the batterylifepercent
information. I can get the batterylifepercent, but I can't get the second
information when I call the secound function "GetBattFlag". Here is the code :

-------------------------------------------------
// GetPowerBatt.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "Winbase.h"
#include "tchar.h"
#pragma comment(lib, "coredll.lib")

TCHAR a_szSUC[] = _T("1");
TCHAR a_szERR[] = _T("0");

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

int GetInfosBatt()
{
SYSTEM_POWER_STATUS_EX powerStatusEx;
int nBattValue = 0;

BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
if(fRet)
{
nBattValue = powerStatusEx.BatteryLifePercent;
return nBattValue;
}
return -1;
}

int GetFlagBatt()
{
SYSTEM_POWER_STATUS_EX powerStatusEx;
int nFlagBatt = 0;

BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
if(fRet)
{
nFlagBatt = powerStatusEx.BatteryFlag;
return nFlagBatt;
}
return -1;
}


///////////////////////////////////////////////////////
// Function: Retourne le pourcentage de batterie restante.
//
// Parameters:
// 1 [OUT] - Value
// 2 [OUT] - Result
extern "C" void GetBattInfos(TCHAR** pData)
{
static TCHAR pszData[33];
int flagVal = GetInfosBatt();
LPTSTR str1 = _T("");
char *buffer1 = "";

if(flagVal != -1)
pData[1] = a_szSUC;
else
pData[1] = a_szERR;

_itoa(flagVal, buffer1, 10);
int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
pData[0] = str1;
}

///////////////////////////////////////////////////////
// Function: Retourne le flag de batterie disant si cell-ci est sur le puits
ou pas.
//
// Parameters:
// 1 [OUT] - Value
// 2 [OUT] - Result
extern "C" void GetBattFlag(TCHAR** pData)
{
int flagBatt = GetFlagBatt();
LPTSTR str2 = _T("");
char *buffer2 = "";

if(flagBatt != -1)
pData[1] = a_szSUC;
else
pData[1] = a_szERR;

_itoa(flagBatt, buffer2, 10);
int size2 = MultiByteToWideChar(CP_ACP, 0, buffer2, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, buffer2, -1, str2, size2);
pData[0] = str2;
}
-------------------------------------------------------

Thanks for any help.

RE: Problem with a dll on PPC 2002 by Gandalf

Gandalf
Mon May 30 05:14:03 CDT 2005

In fact, I modified the dll and I call only the following function :

---------------------------------------------
extern "C" void GetBattInfos(TCHAR** pData)
{
int flagVal = 0;
int flagBatt = 0;
LPTSTR str1 = _T("");
LPTSTR str2 = _T("");
char *buffer1 = "";
char *buffer2 = "";

flagVal = GetInfosBatt();
flagBatt = GetFlagBatt();

if(flagVal != -1 && flagBatt != -1)
{
pData[2] = a_szSUC;
}
else
pData[2] = a_szERR;

_itoa(flagVal, buffer1, 10);
int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
pData[0] = str1;

_itoa(flagBatt, buffer2, 10);
int size2 = MultiByteToWideChar(CP_ACP, 0, buffer2, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, buffer2, -1, str2, size2);
pData[1] = str2;
}
---------------------------------------------------------

The problem : the value I get, especially "str1" has an incorrect value.
For example, I get "911" whereas I should get "91", "711" whereas I should
get "71" and so one...I have always a "1" on the end of chain which should
not be present...

If someone sees where is the problem ?
Thanks in advance.


"Gandalf" wrote:

> Hi,
>
> I have created a dll which permit to get two informations, the flag saying
> if the PDA is in charge and the flag which is giving the batterylifepercent
> information. I can get the batterylifepercent, but I can't get the second
> information when I call the secound function "GetBattFlag". Here is the code :
>
> -------------------------------------------------
> // GetPowerBatt.cpp : Defines the entry point for the DLL application.
> //
>
> #include "stdafx.h"
> #include "Winbase.h"
> #include "tchar.h"
> #pragma comment(lib, "coredll.lib")
>
> TCHAR a_szSUC[] = _T("1");
> TCHAR a_szERR[] = _T("0");
>
> BOOL APIENTRY DllMain( HANDLE hModule,
> DWORD ul_reason_for_call,
> LPVOID lpReserved
> )
> {
> return TRUE;
> }
>
> int GetInfosBatt()
> {
> SYSTEM_POWER_STATUS_EX powerStatusEx;
> int nBattValue = 0;
>
> BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> if(fRet)
> {
> nBattValue = powerStatusEx.BatteryLifePercent;
> return nBattValue;
> }
> return -1;
> }
>
> int GetFlagBatt()
> {
> SYSTEM_POWER_STATUS_EX powerStatusEx;
> int nFlagBatt = 0;
>
> BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> if(fRet)
> {
> nFlagBatt = powerStatusEx.BatteryFlag;
> return nFlagBatt;
> }
> return -1;
> }
>
>
> ///////////////////////////////////////////////////////
> // Function: Retourne le pourcentage de batterie restante.
> //
> // Parameters:
> // 1 [OUT] - Value
> // 2 [OUT] - Result
> extern "C" void GetBattInfos(TCHAR** pData)
> {
> static TCHAR pszData[33];
> int flagVal = GetInfosBatt();
> LPTSTR str1 = _T("");
> char *buffer1 = "";
>
> if(flagVal != -1)
> pData[1] = a_szSUC;
> else
> pData[1] = a_szERR;
>
> _itoa(flagVal, buffer1, 10);
> int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
> MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
> pData[0] = str1;
> }
>
> ///////////////////////////////////////////////////////
> // Function: Retourne le flag de batterie disant si cell-ci est sur le puits
> ou pas.
> //
> // Parameters:
> // 1 [OUT] - Value
> // 2 [OUT] - Result
> extern "C" void GetBattFlag(TCHAR** pData)
> {
> int flagBatt = GetFlagBatt();
> LPTSTR str2 = _T("");
> char *buffer2 = "";
>
> if(flagBatt != -1)
> pData[1] = a_szSUC;
> else
> pData[1] = a_szERR;
>
> _itoa(flagBatt, buffer2, 10);
> int size2 = MultiByteToWideChar(CP_ACP, 0, buffer2, -1, NULL, 0);
> MultiByteToWideChar(CP_ACP, 0, buffer2, -1, str2, size2);
> pData[0] = str2;
> }
> -------------------------------------------------------
>
> Thanks for any help.

Re: Problem with a sll on PPC 2002 by ctacke/>

ctacke/>
Mon May 30 06:51:35 CDT 2005

Well what you're trying to do just generally confuses me. What are you
trying to do with the TCHAR**? Why not use two IN/OUT parameters, one for
each piece of data, and both TCHAR *s?

At any rate, even keeping your achitecture you have problems. Let's just
look at this one:

extern "C" void GetBattInfos(TCHAR** pData)
{
static TCHAR pszData[33];
int flagVal = GetInfosBatt();
LPTSTR str1 = _T("");
char *buffer1 = "";

if(flagVal != -1)
pData[1] = a_szSUC;
else
pData[1] = a_szERR;

_itoa(flagVal, buffer1, 10);
int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
pData[0] = str1;
}

First, this is a problem:

int flagVal = GetInfosBatt();
char *buffer1 = "";
...
_itoa(flagVal, buffer1, 10);

You've not allocated any space for data beyond a null ternimator in buffer1,
so this will step on unallocated memory.

You again make the same error here:

LPTSTR str1 = _T("");
...
MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);

And why don't you just get the string in TCHARS in the first place by using
_itow or _itot?

The last problem is that you assign the address of your return pointer
pData[0] to a locally declared variable, which means that when this function
ends, it's no longer valid. The caller should be allocating space and
passing in preallocated buffers for the data.

-Chris


"Gandalf" <Gandalf@discussions.microsoft.com> wrote in message
news:93E549BA-41A5-4735-B2AD-5462925632B2@microsoft.com...
> Hi,
>
> I have created a dll which permit to get two informations, the flag saying
> if the PDA is in charge and the flag which is giving the
> batterylifepercent
> information. I can get the batterylifepercent, but I can't get the second
> information when I call the secound function "GetBattFlag". Here is the
> code :
>
> -------------------------------------------------
> // GetPowerBatt.cpp : Defines the entry point for the DLL application.
> //
>
> #include "stdafx.h"
> #include "Winbase.h"
> #include "tchar.h"
> #pragma comment(lib, "coredll.lib")
>
> TCHAR a_szSUC[] = _T("1");
> TCHAR a_szERR[] = _T("0");
>
> BOOL APIENTRY DllMain( HANDLE hModule,
> DWORD ul_reason_for_call,
> LPVOID lpReserved
> )
> {
> return TRUE;
> }
>
> int GetInfosBatt()
> {
> SYSTEM_POWER_STATUS_EX powerStatusEx;
> int nBattValue = 0;
>
> BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> if(fRet)
> {
> nBattValue = powerStatusEx.BatteryLifePercent;
> return nBattValue;
> }
> return -1;
> }
>
> int GetFlagBatt()
> {
> SYSTEM_POWER_STATUS_EX powerStatusEx;
> int nFlagBatt = 0;
>
> BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> if(fRet)
> {
> nFlagBatt = powerStatusEx.BatteryFlag;
> return nFlagBatt;
> }
> return -1;
> }
>
>
> ///////////////////////////////////////////////////////
> // Function: Retourne le pourcentage de batterie restante.
> //
> // Parameters:
> // 1 [OUT] - Value
> // 2 [OUT] - Result
> extern "C" void GetBattInfos(TCHAR** pData)
> {
> static TCHAR pszData[33];
> int flagVal = GetInfosBatt();
> LPTSTR str1 = _T("");
> char *buffer1 = "";
>
> if(flagVal != -1)
> pData[1] = a_szSUC;
> else
> pData[1] = a_szERR;
>
> _itoa(flagVal, buffer1, 10);
> int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
> MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
> pData[0] = str1;
> }
>
> ///////////////////////////////////////////////////////
> // Function: Retourne le flag de batterie disant si cell-ci est sur le
> puits
> ou pas.
> //
> // Parameters:
> // 1 [OUT] - Value
> // 2 [OUT] - Result
> extern "C" void GetBattFlag(TCHAR** pData)
> {
> int flagBatt = GetFlagBatt();
> LPTSTR str2 = _T("");
> char *buffer2 = "";
>
> if(flagBatt != -1)
> pData[1] = a_szSUC;
> else
> pData[1] = a_szERR;
>
> _itoa(flagBatt, buffer2, 10);
> int size2 = MultiByteToWideChar(CP_ACP, 0, buffer2, -1, NULL, 0);
> MultiByteToWideChar(CP_ACP, 0, buffer2, -1, str2, size2);
> pData[0] = str2;
> }
> -------------------------------------------------------
>
> Thanks for any help.



Re: Problem with a sll on PPC 2002 by Gernot

Gernot
Mon May 30 07:25:19 CDT 2005

hint: never do this:

TCHAR* name=_T("A name");
_tcscpy(name, _T("XY"));

always do this:

TCHAR name[]="A name";
_tcscpy(name, _T("XY"));

Because, depending on compiler and operating system the first one will
point to protected memory, which will cause your app to crash. The
latter allocates the string in data memory.

just my .02$,
Gernot



Re: Problem with a sll on PPC 2002 by Gandalf

Gandalf
Mon May 30 08:13:39 CDT 2005

Thanks Chris for your information, I changed the following code :
Now I make :

int flagVal = 0;
char buffer1[3];
...
flagVal = GetInfosBatt();
...

Is this first step better ?

Then, I need to have an output variable of type TCHAR**, because I'm using a
specific AGL...
So how can I convert "buffer1" variable to TCHAR ? By using _itow function ?
Thanks for any example !


"<ctacke/>" wrote:

> Well what you're trying to do just generally confuses me. What are you
> trying to do with the TCHAR**? Why not use two IN/OUT parameters, one for
> each piece of data, and both TCHAR *s?
>
> At any rate, even keeping your achitecture you have problems. Let's just
> look at this one:
>
> extern "C" void GetBattInfos(TCHAR** pData)
> {
> static TCHAR pszData[33];
> int flagVal = GetInfosBatt();
> LPTSTR str1 = _T("");
> char *buffer1 = "";
>
> if(flagVal != -1)
> pData[1] = a_szSUC;
> else
> pData[1] = a_szERR;
>
> _itoa(flagVal, buffer1, 10);
> int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
> MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
> pData[0] = str1;
> }
>
> First, this is a problem:
>
> int flagVal = GetInfosBatt();
> char *buffer1 = "";
> ....
> _itoa(flagVal, buffer1, 10);
>
> You've not allocated any space for data beyond a null ternimator in buffer1,
> so this will step on unallocated memory.
>
> You again make the same error here:
>
> LPTSTR str1 = _T("");
> ....
> MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
>
> And why don't you just get the string in TCHARS in the first place by using
> _itow or _itot?
>
> The last problem is that you assign the address of your return pointer
> pData[0] to a locally declared variable, which means that when this function
> ends, it's no longer valid. The caller should be allocating space and
> passing in preallocated buffers for the data.
>
> -Chris
>
>
> "Gandalf" <Gandalf@discussions.microsoft.com> wrote in message
> news:93E549BA-41A5-4735-B2AD-5462925632B2@microsoft.com...
> > Hi,
> >
> > I have created a dll which permit to get two informations, the flag saying
> > if the PDA is in charge and the flag which is giving the
> > batterylifepercent
> > information. I can get the batterylifepercent, but I can't get the second
> > information when I call the secound function "GetBattFlag". Here is the
> > code :
> >
> > -------------------------------------------------
> > // GetPowerBatt.cpp : Defines the entry point for the DLL application.
> > //
> >
> > #include "stdafx.h"
> > #include "Winbase.h"
> > #include "tchar.h"
> > #pragma comment(lib, "coredll.lib")
> >
> > TCHAR a_szSUC[] = _T("1");
> > TCHAR a_szERR[] = _T("0");
> >
> > BOOL APIENTRY DllMain( HANDLE hModule,
> > DWORD ul_reason_for_call,
> > LPVOID lpReserved
> > )
> > {
> > return TRUE;
> > }
> >
> > int GetInfosBatt()
> > {
> > SYSTEM_POWER_STATUS_EX powerStatusEx;
> > int nBattValue = 0;
> >
> > BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> > if(fRet)
> > {
> > nBattValue = powerStatusEx.BatteryLifePercent;
> > return nBattValue;
> > }
> > return -1;
> > }
> >
> > int GetFlagBatt()
> > {
> > SYSTEM_POWER_STATUS_EX powerStatusEx;
> > int nFlagBatt = 0;
> >
> > BOOL fRet = GetSystemPowerStatusEx(&powerStatusEx, true);
> > if(fRet)
> > {
> > nFlagBatt = powerStatusEx.BatteryFlag;
> > return nFlagBatt;
> > }
> > return -1;
> > }
> >
> >
> > ///////////////////////////////////////////////////////
> > // Function: Retourne le pourcentage de batterie restante.
> > //
> > // Parameters:
> > // 1 [OUT] - Value
> > // 2 [OUT] - Result
> > extern "C" void GetBattInfos(TCHAR** pData)
> > {
> > static TCHAR pszData[33];
> > int flagVal = GetInfosBatt();
> > LPTSTR str1 = _T("");
> > char *buffer1 = "";
> >
> > if(flagVal != -1)
> > pData[1] = a_szSUC;
> > else
> > pData[1] = a_szERR;
> >
> > _itoa(flagVal, buffer1, 10);
> > int size1 = MultiByteToWideChar(CP_ACP, 0, buffer1, -1, NULL, 0);
> > MultiByteToWideChar(CP_ACP, 0, buffer1, -1, str1, size1);
> > pData[0] = str1;
> > }
> >
> > ///////////////////////////////////////////////////////
> > // Function: Retourne le flag de batterie disant si cell-ci est sur le
> > puits
> > ou pas.
> > //
> > // Parameters:
> > // 1 [OUT] - Value
> > // 2 [OUT] - Result
> > extern "C" void GetBattFlag(TCHAR** pData)
> > {
> > int flagBatt = GetFlagBatt();
> > LPTSTR str2 = _T("");
> > char *buffer2 = "";
> >
> > if(flagBatt != -1)
> > pData[1] = a_szSUC;
> > else
> > pData[1] = a_szERR;
> >
> > _itoa(flagBatt, buffer2, 10);
> > int size2 = MultiByteToWideChar(CP_ACP, 0, buffer2, -1, NULL, 0);
> > MultiByteToWideChar(CP_ACP, 0, buffer2, -1, str2, size2);
> > pData[0] = str2;
> > }
> > -------------------------------------------------------
> >
> > Thanks for any help.
>
>
>