-
February 14th, 2007, 10:21 AM
#1
Char to LPCSTR
hi all
how can convert of Char To LPCSTR,
well, I am developing a DLL to use it in VB and this is a function that I am building.
I try this, but it gives me bad result .
Code:
void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
, long *nCant)
{
char *res;
GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
*Valor=(LPCSTR )res; --here is the problem
}
DLL is created Ok.
but when this function is used in VB, *Valor return anything less value I hope
PDT. if anyone knows about develop DLL with C++ to user it in VB,
could help me out with some links I can use like guide.
-
February 14th, 2007, 02:10 PM
#2
Re: Char to LPCSTR
Hello,
I don't know about VB. It looks like you know the same thing about C++.
There are too many mistakes in the code you posted. For instance:
Code:
char *res;
GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
When you declare a pointer to char it means you get exactly what you requested: a pointer to char; you do not get a pointer and space to store some characters. GetPrivateProfileString will try to store the result into the space pointed by res. But res points to a random location.
You must read a bit (more) about C++. Sorry...
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
February 14th, 2007, 05:10 PM
#3
Re: Char to LPCSTR
the function in your code does not allocate space for you..
why put this in a function ? they are axaclty the same
-
February 14th, 2007, 11:28 PM
#4
Re: Char to LPCSTR
Originally Posted by zdf
Hello,
I don't know about VB. It looks like you know the same thing about C++.
There are too many mistakes in the code you posted. For instance:
Code:
char *res;
GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
When you declare a pointer to char it means you get exactly what you requested: a pointer to char; you do not get a pointer and space to store some characters. GetPrivateProfileString will try to store the result into the space pointed by res. But res points to a random location.
You must read a bit (more) about C++. Sorry...
thanks for reply, but I studied Borland c++ 3.0 before, but I have problem to develop DLL because I never have done it.
I understand that you mean me. I have to initialize 'res'.
Last edited by hensa22; February 14th, 2007 at 11:31 PM.
-
February 14th, 2007, 11:41 PM
#5
Re: Char to LPCSTR
Code:
void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
, long nCant)
{
char *res;
res=new char[nCant];
GetPrivateProfileString(*Seccion, *Key,"-1", res, nCant, *FileIni );
*Valor=(LPCSTR )res;
}
it's ok.
it works perfectly in VB
thanks.
-
February 15th, 2007, 01:49 AM
#6
Re: Char to LPCSTR
Hello,
There are still errors in your code. For instance: LPCTSTR is a pointer to a vector of constant TCHAR. TCHAR is different from char, that is “*Valor=(LPCSTR )res;” is not all right.
Don’t forget to release the memory.
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
February 15th, 2007, 06:29 AM
#7
Re: Char to LPCSTR
LPCSTR is a pointer to memory which is used as an array of constant char's
the code is perfectly fine.is this C or C++
-
February 15th, 2007, 03:46 PM
#8
Re: Char to LPCSTR
I am sorry but I do not understand. Why is it “perfectly fine”?
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
February 16th, 2007, 09:30 AM
#9
Re: Char to LPCSTR
Originally Posted by hensa22
Code:
void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
, long nCant)
{
char *res;
res=new char[nCant];
GetPrivateProfileString(*Seccion, *Key,"-1", res, nCant, *FileIni );
*Valor=(LPCSTR )res;
}
this code above is still little bad.
Code:
long LeerIni(LPSTR *Valor,const LPCSTR &Seccion, LPCSTR &Key, LPCSTR &FileIni,long &nCant)
{
long c=-1;
c=GetPrivateProfileString(Seccion, Key,"-1",*Valor,nCant,FileIni);
return c;
}
it's at 100% ok.it works in VB without problem.
-
February 16th, 2007, 10:23 AM
#10
Re: Char to LPCSTR
oh man i missed that mistake, i hate microsoft defines they are so hard to detect mistakes.
also why do make a temp var?
just make Valor = new char instead of a temp
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|