|
Hi
Thanks for the reply. I am just curious, why's global constants such a travesty in C++? I always thought they're the preferred replacements for #define preprocessor? What's the advantage that I gain by defining them as static? Thanks!
|
|
|
|
|
Because he once read that globals where bad and doesn't really understand why.
There is nothing wrong with global constants. As you noted, they are the repacement of #define.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Tim Smith wrote:
There is nothing wrong with global constants. As you noted, they are the repacement of #define.
2nd on that! I always prefer static const globals over #define s as they are type-checked and that is solid ground for morally sound C++ coding.
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
Maybe I was a bit harsh there, but under the princliple of OOP you are supposed to encapslate everything including const data. This is a purist's view, so don't take any mind if you approaching it from a practical standpoint. static will ensure that the data is instantiated in the data segment of your software, rather than on the heap or stack. That goes back to your question on when it is created.
-----------------------------
All truth passes through 3 stages.
First, it is ridiculed.
Second, it is violently opposed.
Third, it is accepted as being self-evident.
|
|
|
|
|
Indrawati wrote:
const char* A_STRING = "abcdef";
This defines a variable that is a pointer to a constant string. The pointer can be changed to point to different locations, but the value at that location can't be changed. So in effect, it's not really a constant at all. The compiler complains because you've got a multiply-initialised variable defined in multiple files.
Indrawati wrote:
const char *const A_STRING = "abcdef";
This truly is constant, so the compiler can safely optimise it out and prevent multiple definitions.
As was said above, adding static to the front of the definition will prevent the multiple-definition problem.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
HI
I am writing a DLL in which i am creating a thread like this:
extern "C" __declspec(dllexport) BOOL ReadCard(char *pData)
{
if(g_hPort == NULL)
{
strcpy(pData,"COM port is not opened");
return false;
}
g_hThreadRead = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFunc, 0, 0 ,&dwThreadID);
return true;
}
This create thread function is suppose to go to ThreadFunc() but it is not going . can any body give me some suggestion why it is not going to ThreadFunc()
DWORD WINAPI ThreadFunc(LPVOID pData)
{
BOOL bSuccess = TRUE;
while(1)
{
int index = 0;
// Clear the dara buffer first.
memset(g_strResult,0x00,500);
if ( l_bTaskAbort )
{
Disconnect();
return(false);
}
//if card is seated
// Ask the user to remove the card
// if the card is removed then read the data
if(GetCardData())
{
//MyMSR_ShowResult(0);
strcpy((char*)pData,g_strResult);
break;
}
}
return bSuccess;
}
Thanks in advance
shailesh
|
|
|
|
|
Why aren't you checking the return value from CreateThread? If a NULL is returned, GetLastError will return information on why it failed. It can't hurt to try that.
Also, try to avoid using (LPTHREAD_START_ROUTINE) cast. If you can't use the routine name without the cast, then chances are your routine is declared wrong and the thread might fail in strange ways.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I Checked the returned value from CreateThread, it is a handle to the thread but it is not going to the function. Also i Called the GetLastError() after the createthread() function it is returning zero.
Any more suggestions...
Thanks
Shailesh
|
|
|
|
|
I am automating word to create mailing labels from a text file using mfc. I am having a problem when I bring up a new instance of word (the one which I am automating) and another (non-automation) instance is already running. I get a box to save normal.dot, I have tried Microsofts fix to delete the autosave version ~normal.dot but this does not work. Any suggestions would be greatly appreciated.
cheers,
Andy
carpe diem!
|
|
|
|
|
I have code that uses
ExecuteSQL( LPCSTR lpszSQL );
to Insert into rows in a MS Access database.
It works well 99.9% of the time. However, once in a great while the sql string to be inserted contains a single quote in the values field.
I do not want to mask out the quote, is there a sequel encode function I can use to write values like [Dave's object']?
thanks
|
|
|
|
|
Replace the ' with '' (that's two ' s)
modified 12-Jul-20 21:01pm.
|
|
|
|
|
|
I usually use the ` character (0x60, grave accent) which has the advantage to be graphically closer than the double quote.
|
|
|
|
|
Thank you - that is a good suggestion.
|
|
|
|
|
I believe I have a simple problem or a novice problem.
If I write a DLL using Borland C++, and want to call another DLL from within the running DLL, what is the format to do so?
Can I get an example?
Problem:
I have a program that calls one DLL to get a piece of data.
Then I use that (let us use an interger ) data to call a 2nd DLL.
What I would like to do is when I call the 2nd DLL, I would like that DLL to call the first DLL to get the interger data and continue to process and then return to the original calling program.
This appears to be a simple call format inside the 2nd DLL, but I am going in circles.
I have been searching the sites, and all problems seem so much more complex than what I am looking for.
Thanks.
|
|
|
|
|
george33027 wrote:
...want to call another DLL
There is no such thing as "calling a DLL." You can dynamically link with another DLL using LoadLibrary() (and then one or more calls to GetProcAddress() ), or you can call a function exported by the DLL regardless of whether you statically or dynamically linked.
george33027 wrote:
What I would like to do is when I call the 2nd DLL, I would like that DLL to call the first DLL to get the interger data and continue to process and then return to the original calling program.
Let me get this straight. The 1st DLL is going to call an exported function within the 2nd DLL which is in turn going to call an exported function within the 1st DLL (to get an integer). Is that right? If not, you'll need to be a bit clearer on which DLL is doing what.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
I guess I got my terminology wrong.
Let me restate, because I think you gave me almost the answer I need.
Thank you so far.
If I make a DLL (call it number1.DLL) whose export will be a constant, or the DLL will return a constant.
Now I make a 2nd DLL let us call it number2.DLL.
In number2.DLL I want to link number1.DLL so that I can get that constant to use.
So how would you set up the program.cpp file to like the number1.DLL ?
#include <windows.h>
extern "C"
__export WINAPI test()
{
// code to get the constant from \path\number1.DLL
return(Number from number1.dll);
}
How would you put in the LoadLibrary() to get to a specific path and number1.DLL.
Then how would you use the GetProcAddress() to get this constant.
|
|
|
|
|
Let me recompose the statement in a bit different manner..
You have program, call it Proggy, for now. Proggy loads only the second DLL, and uses GetProcAddress to get the exported function. When this exported function is called, the second DLL loads the first DLL in order to retrieve the required constant. Then the second DLL returns this constant to Proggy.
Is this logic correct ? If so, the steps to implement it are as follows:
1. Create the first DLL. It has a function funcXYZ that is exported and returns the constant.
2. Create the second DLL. It has a function func123 that is also exported, and returns a number.
3. The function inside the second DLL uses LoadLibrary to load the first DLL, and then GetProcAddress to obtain access to the function exported by the first DLL.
4. This function-pointer is used to call the exported function.
5. As you now have the number, you can use FreeLibrary to unload the first DLL. At this point, the function pointer becomes invalid.
6. Return the number from the second DLL's exported function just like you'd return any other number. See here, that in the second DLL's function, you need to create a local variable that will hold a copy of the constant delivered from the first DLL.
Here's a code example of the first DLL's function:
__declspec(dllexport) int ReturnConstant(void)
{
return TheConstant;
} Here is the function in the second DLL:
__declspec(dllexport) int GetNumber(void)
{
HMODULE hMod = LoadLibrary("Number1.DLL");<DIV>
int (*ptrFunc)(void) = (int (*)(void)) GetProcAddress(hMod, "ReturnConstant");<DIV>
const int theConstant = ptrFunc();<DIV>
FreeLibrary(hMod);
ptrFunc = NULL;<DIV>
return theConstant;
} Note that I've used a direct pointer-to-function declaration with GetProcAddress . It might be easier for you to use a typedef to declare the type first. See MSDN for an example of how typedef is used to declare pointer-to-function types.
Note also that I've used Microsoft Visual C++ to declare the exported functions. The __declspec identifier is not available in Borland C++, you must use some other keyword, like the __export .
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Thank you Thank you Thank you.
This worked perfectly.
Thank you Antti.
One more question, is there anyway to put in a path in the LoadLibrary(path"Numer1.DLL");
All works well if the dll is in the windows folder, but I would like to put the first DLL with my other DLL's.
Is this possible within the def ?
|
|
|
|
|
The LoadLibrary will search for the DLL, if the path is not specified, in the following order:
1. Current directory
2. Windows\System
3. Other misc directories
You can specify a path into the string, but for all backslash characters, you need to use a double-backslash, i.e. "C:\\Windows\\System\\Numer1.DLL". If you are running inside the first DLL and wish to know the directory where you are now, use GetModuleFileName . Then parse this path, adding or removing file names as necessary to identify the path/name of the second DLL.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Antti;
You are good!
On another topic.
How would you get specific computer information, like the maker of the motherboard, bios date, serial number of hardware, etc ?
Basically, a simple way to identify a specific computer, without going to alot of trouble.
Using basic PC's, using MS Windows (all versions)
|
|
|
|
|
You should use Windows Management Instrumentation (WMI).
It provides access to details of what processor, OS, motherboard and other such components a computer has. Many programs like SiSoft Sandra use WMI to get details of a computer in an orderly fashion.
I'll provide a link to the MSDN Library, on the WMI start page here[^]. You should read and familiarize yourself before actually using them, because incorrect usage may lead to problematic situations.
The most interesting things you can query from the WMI (and the ones you're interested in) are located here[^].
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Now I would like to get text information from an internet site.
Do you have an example of how I would connect to an internet site and download a specfic file?
Assume that I am connected to the internet and I am using another program to download data.
Now when in my DLL, I need some specific information, I would like to connect to a specific site (I know the IP address and folder)and download a file and check some text information.
Can I get an example.
|
|
|
|
|
I wish to read and write NTFS extended file attributes in C++.
I was very surprised that I did not find this information readily available.
First I am assuming that NTFS extended file attributes are name-value pairs of
strings. Is this correct? May I create a name and a value and attach that to
a file as an extended file attribute?
Presuming that that is correct, how do I
1) Determine whether a file supports extended file attributes (resides
on a NTFS drive)
2) Read the extended file attributes
3) Set the extended file attributes
I am in a VC++ 6 environment.
Thanks
|
|
|
|
|
gokings wrote:
...NTFS extended file attributes...
Just so we are on the same page here, what is your definition of "NTFS extended file attributes."
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|