|
thanks Marc ! that was the key ! now it is working fine !!!
thanks again for your help !!!
ganralf
|
|
|
|
|
Also note that in my last post, the comments apply to __stdcall
functions only.
__cdecl functions are exported without decorations so you can use
__declspec(dllexport) and not worry about it.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hallo,
I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this:
MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....
All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why????
Please help to find my error`...
|
|
|
|
|
This happens because you said it was static. That means it there is a static instance of it in every module that includes MyApp.h.
If you want the variable to be global and shared among the modules of the app then it can not be static.
|
|
|
|
|
Thanks your for your answer. But when I delete the static keyword, I get a lot of compiler errors like (...already defined in ... object).
|
|
|
|
|
Are the globals changed at run-time? If they aren't you can substitute them with defines, for example:
bool bSetOutputToDebugWindow = false;
becomes
#define SETOUTPUTTODEBUGWINDOW 0
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
I usually put the declaration (extern bool bVar; ) in the common header file and in one of the cpp files (in your case MyApp.cpp)I do create the variable ( bool bVar; ).
That guarantees that I'm using the same variable across my cpp files. but personally I try to avoid using global variables. A better approach would be static member variable.
|
|
|
|
|
You should not be, under any circumstances, declaring a variable in a header file, unless it is inside a function, class, or struct declaration. That is how you get the compile errors you were talking about when you remove the static keyword
This resource[^] may be useful to you.
|
|
|
|
|
Instead of putting as static, you can use #ifndef compiler option as follows in header file.
globals.h
+++++++++
#ifndef _myglobals_h
#define _myglobals_h
.................
declare variables
...............
#endif
-Parthi
|
|
|
|
|
You should not do that.
Instead:
(1) declare your variable as extern in the (commonly included) header file, for instance in MyApp.h:
extern bool bSetOutputToDebugWindow;
(2) define the variable only once, inside one source file, for instance MyApp.cpp (please note I did not use the static keyword):
bool bSetOutputToDebugWindow;
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]
|
|
|
|
|
Dear All
In visual C++ 2005
I want to change a CString file to char*
I used following code
But fopen needs a char* for its first parameter
Please let me know what to do
Regards
char buffer[MAX_PATH];
GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);
CString path = buffer;
path = path.Left(path.ReverseFind('\\')+1);
path += "result.mp3";
f = fopen( path , "wb");
|
|
|
|
|
It should do.
So, what's wrong ? Do you get an error ?
CString has an inner cast Operator (LPCTSTR ) which converts the CString object to a const char* (if compiling ANSI) of const wchar_t* (if building unicode), so you should not have to do any much stuff.
And please (if you get this advice from anywhere), don't use the CString::GetBuffer() ) method at all for such a thing !!!
So, to come back to you problem, what is your problem ?
|
|
|
|
|
Hi super_ttd,
What's the problem with CString::GetBuffer()? Why dont you like it?
|
|
|
|
|
GetBuffer() is completely unnecessary when you're only reading the string
(i.e. when you only need a const pointer to the string's internal buffer).
The LPCSTR operator is safe and more efficient. GetBuffer() is for when
you need a non-const pointer.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Same ocean, different fish, same bait.
You have lunch yet?
led mike
|
|
|
|
|
led mike wrote: You have lunch yet?
Heh. I'm going to HAVE to now
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator.
isn't it?
Regards
|
|
|
|
|
Yes, it is simply because UNICODE is defined by default (which was not the case with VC6). I strongly suggest that you read the article given by Led Mike, things will be much more clear afterwards.
|
|
|
|
|
The operator is a member of the CSimpleStringT class, which is the base
class of CStringT.
The actual operator is PCXSTR (the ATL type), which is the equivalent of LPCTSTR
(a Win32 type).
Here's your code using generic text types:
TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,MAX_PATH);
CString path = buffer;
path = path.Left(path.ReverseFind(_T('\\'))+1);
path += _T("result.mp3");
f = _tfopen( path , _T("wb")); <code>
See Generic-Text Mappings in Tchar.h[^]
and for Unicode info: International Programming[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Read the documentation about CString::GetBuffer and you will see why you have to absolutely avoids it if you don't know exactly what you are doing.
|
|
|
|
|
Visual C++ 2005 doesn't support (LPCTSTR) cast operator.
thus I need an interface to change a CString object to a char*
exactly now I am porting an project from VC6 to VC2005 and it is my problem
Regards
Mahdi
|
|
|
|
|
There a various ways:
1)
CString str = "filename";
char charPtr[100];
sprintf(charPtr, "%s", str);
2)
CString str = "filename";
char* charPtr = str.GetBuffer(str.GetLength());
|
|
|
|
|
zafersavas wrote: str.GetBuffer(str.GetLength());
Memory Leak here !
Why the hell do people use that dangerous GetBuffer() function when it's absolutely useless ?
|
|
|
|
|
Gut Mikh Tappe wrote: Please let me know what to do
Ok I will try. What you need to do is read the documentation.[^]
Also reading this might help as well.[^]
In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.
led mike
|
|
|
|