|
Hi guys I have a simple question for your.
Let say that I have CreateFile function that takes as a first argument a LPCTSTR pointer.
Also there is a declared buffer as char* szBuffer . I' ve seen a lot of guys using a typecasting
for calling CreateFile like this CreateFile((LPCTSTR)szBuffer,....). I simply use
CreateFile(szBuffer,....) with no problems
Is that typecasting needed, given that both char* and LPCTSTR are pointers. Are there any true gains by
using typecasting?
Thanx
|
|
|
|
|
FotisSs wrote: I' ve seen a lot of guys using a typecasting
Well, they were wrong... LPCTSTR is an array of TCHAR and the definition of a TCHAR depends if UNICODE is enabled or not. So, the code will work fine if UNICODE is not defined but it will not work if UNICODE is defined. In which case, if you have the typecast, it will simply be plain wrong (the string will be corrupted), but if you don't have the cast, then it will not compile (which is way better, because at least you don't have a nasty bug in your code). Typecasts should be avoided at maximum: using them too much means probably that you don't understand what you are doing.
I suggest that you read this article[^], it explains a lot about character encoding (and about TCHAR).
|
|
|
|
|
Usage of things like char is what scaring me more than the typecasts. One should ideally use TCHAR instead, which is neutral to builds (and is defined as char in an ANSI build and as a wchar_t in an Unicode build).
If an API expects an LPCTSTR , that means that it has both an ANSI and an Unicode implementation and the appropriate version will be called based on the build (for Unicode build, CreateFileW and for an ANSI build, CreateFileA will be called).
There is no excuse to use char unless you're *sure* that you are always going to do an ANSI only build. Even then, I won't recommend it.
Typecasting a char* to an LPCTSTR in an Unicode build is an invitation to disaster, because the wide version of the API will be called, which is going to expect a wchar_t* .
There is no excuse to write such crap code.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
ok my fault I always tend to use specifically CreateFileA so it is LPSTR!
And the question remains is typecasting needed?
|
|
|
|
|
FotisSs wrote: And the question remains is typecasting needed?
I haven't answered it?
Take CreateFile for example, which expects an LPCTSTR
If you have a TCHAR* , no typecasting is needed, or typecasting has no effect.
If you have a char* , passing that to this function by casting it to an LPCTSTR will cause trouble in Unicode Build.
If you have a wchar_t* , passing that to this function by casting it to an LPCTSTR will cause trouble in ANSI build.
If you use TCHAR* , no casting will be needed at all, and the correct data type will be automatically used.
I will also highly recommend that you read the article that the other poster gave you a link for! It's well worth the time you'll spend on it.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
FotisSs wrote: Is that typecasting needed, given that both char* and LPCTSTR are pointers.
Unicode issues aside, no, and not because they are both pointers. It's because the function's signature will treat the pointer argument as a const and not change it.
On the other hand, if you had a function that was expecting a LPTSTR and you were passing it a const pointer, then casting would be an issue.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I have a dll whose name conflicts with that of a system dll.
Mydll was created by me and although I have the code with me I do have other projects that link to this dll.
Now I need to modify the code in the exe that links to this dll.
However I can't run the exe since now the application tries to link to the dll and connects to the system dll instead of mydll.
The dll is in a specific folder and not the application's home directory; along with the other dlls that are rerferred to in the application.
How do I specify to the exe to link to mydll instead of the system dll.
The Build goes fine but the during Run; it stops at the startup since it can't find teh functions in the system dll.
I am using the Visual Studio 6.0 IDE.
|
|
|
|
|
Can't you just rename the DLL so that it doesn't conflict with a system DLL?! That would be the ideal solution.
Alternatively, you could explicitly load your DLL (lookup LoadLibrary[^] and GetProcAddress[^] ) instead of using the .lib file.
But I strongly suggest that you do not have DLL names that conflict with the system DLLs.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Yes I agree that renaming the dll to something other than the system dll will be the ideal solution and that's what I have implemented as a workaround but that is not something that is recommended since the rest of the applications in my project do link to the same dll.
So changing the name of the dll won't quite be the accepted solution across the teams.
Is there any waay to override the system directory path for linking to the dlls.
Like in my case all the dlls are placed in a particular folder on the drive other than the system directory.
If that could be done the issue should be resolved.
|
|
|
|
|
hi every body...
is there any free PDF Printer Drivers or pdf libraries
and i have no idea about virtual printers....
Actuvally i am fresher to joining in a company....
so i have no idea about this things...
please help me....
thanks in advance......
|
|
|
|
|
eswar pothula wrote: is there any free PDF Printer Drivers or pdf libraries
See here[^] for possibilities.
|
|
|
|
|
If you're wanting to do this in C++, then there's LibHaru[^]
You might also want to take a look at List of PDF software[^]
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hello,
I am new in C and i am looking for some example codes of how can i implement the Block Worlds Problem for my exams.
I dont want you to give me the solution but some examples based on this problem to get me started
P.S. Dont try to explain to me with words i already have read the theory, all i need is an idea of how to start this project.
Thank you for your time
Jim
|
|
|
|
|
Member 2384838 wrote: Dont try to explain to me with words i already have read the theory
How (the fresh hell) can we guess what you have already read of the theory?
Supposing you know ALL about theory. What are your doubts about? Aren't you able to write a simple C program?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Member 2384838 wrote: P.S. Dont try to explain to me with words i already have read the theory, all i need is an idea of how to start this project.
#include <stdlib.h>
int main()
{
return 0;
}
|
|
|
|
|
Maybe he needs some more includes.
Greetings
Covean
|
|
|
|
|
Covean wrote: Maybe he needs some more includes.
I didn't want to confuse him with too much information.
|
|
|
|
|
I dont like your ironic comments, on the other hand i'm here to get help i am not asking for the global warming solution routine in c!
If you know nothing about c language please it is better to leave the experts to reply to my quote, if again you know (which i doubt), press Ctrl+T and type another website to fool around with.
P.S. Dont forget using namespace std in the attached source above
|
|
|
|
|
Member 2384838 wrote: I dont like your ironic comments...
Richard was being sarcastic, not ironic.
Member 2384838 wrote: If you know nothing about c language please it is better to leave the experts to reply to my quote...
I realize you're new here, but Richard is undeniably one of the more knowledgeable C programmers on this board.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Member 2384838 wrote: I dont like your ironic comments, on the other hand i'm here to get help i am not asking for the global warming solution routine in c!
I was trying to make the point that no-one here is going to write your program for you.
Member 2384838 wrote: If you know nothing about c language please it is better to leave the experts to reply to my quote, if again you know (which i doubt), press Ctrl+T and type another website to fool around with.
I've written one or two C programs in my time so I do know a bit.
Member 2384838 wrote: P.S. Dont forget using namespace std in the attached source above
In a C program?
|
|
|
|
|
I didn't bother reading the entire thread first and just now posted nearly identical code. I removed it so I would not be suspected of plagiarism.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
|
Hi all ...
I m having several solutions containing several VC++ projects depending on each other in .net 2003 IDE.
I have to compile those solutions from command line (for continous intregation and automating the process )or any other way which could be automated and is free.
How can i do that.??
I tried Using Nmake (for a simple solution for testing only )but the problem is that it is giving error "fatal error U1073: don't know how to make 'CI_POC.cpp'"
Can someone help me out ...!!!!!!!!!!!!!
Thanx a zillion in advance ..
It's not enough to be the best, when you have capability to be great....
|
|
|
|
|
You can try devenv.com with /Build commandline option
|
|
|
|
|
sorry but i didn't get what you want to say ....?
It's not enough to be the best, when you have capability to be great....
|
|
|
|