Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variable and arguments to main function

Status
Not open for further replies.

rpk2006

Technical User
Apr 24, 2002
225
IN
I have few questions:

(1) I want to declare a variable which is GLOBAL for the whole project. Where it needs to be declared?

(2) I have added a C source file in my project, and I want to pass a string as an argument to the MAIN function of this C source. Can it be done?

(3) In circumstances where it is not known what will be the size of the returned output, I want to declare a variable length variable, similar to that of VB. Is it possible in VC++.
 
1)
First ask yourself if its really neccessary.
If so, then ask yourself a second time.
If you still find globals is a must you can declare it as a static member of a class.

2) There can be only one (Highlander) main. At least one main being the entry point of your app. If youve incorporated some old code (with main and all) into a new c++ project, you could perhaps put the old code in a separate namespace and declare it in a .h as well.

oldstuff.h
Code:
namespace oldstuff
{
  int main(int argc, char*argv[]);
};
oldstuff.cpp
Code:
namespace oldstuff
{
   // the old main func etc...
};
Calling the old main:
Code:
   #include "oldstuff.h"

   ...
   
   char* argv[2] = { "Foo", "Bar" };
   oldstuff::main(2,argv);


3) Sure, but you have to declare it (which you dont need to in VB). What kind of variable are you talking about?

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
I should put it more clearly.

In my application, I have a feature to receive any clipboard text and then pass this text to a C program's Main Function as an argument. Now the problem is that Clipboard Data can be of varying size.
 
...and of various type too. If its stored as normal text
its not a big deal though, as all you neeed is a pointer to the string.


Code:
if (OpenClipboard())
{

	HANDLE h = 0;
	
	if ((h = GetClipboardData(CF_TEXT))!=0)
	{
		LPCTSTR lptstr = (LPCTSTR) GlobalLock(h); 
		if (lptstr != NULL) 
		{ 
			// Do something with the string ...					
			// ...
			GlobalUnlock(h); 
		}
	}

	CloseClipboard();
}

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Thanks, I did just that way to receive clipboard data, but I want to transfer this text as an argument to the Main Function of a C program.

Example:

void main(<Clipboard data>)
 
Just pass it as you would with any other string, the main's implementation is part of your C++ project, right?

Code:
void main(<Clipboard data>)
Hmmm...normally a main looks like this:
Code:
int main(int argc, char*argv[]);
Where argc is stating how may strings are stored in the argv array.

So assuming you want to send it one single string you could probably do it like this:
Code:
char* argv[1];
argv[0] = const_cast<char*>lptstr;
main(1, argv);
The const_cast is terrible, but since the main excpects a non-const...however if the code is in your project = under your control you could fixe the main
Code:
int main(int argc, const char*argv[]);
and then you dont need the const_cast.

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
But 'argc' and 'argv' receive arguments from command line, whereas I will pass the arguments at run-time, i.e., during program execution.
 
>whereas I will pass the arguments at run-time,

Right. As is done in my previous post.

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Thanks a lot, I will try and reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top