6 Replies - 2362 Views - Last Post: 23 April 2011 - 02:47 PM Rate Topic: -----

#1 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Drag and drop files through argv[]

Posted 23 April 2011 - 05:49 AM

Hi

I'm having trouble getting useful information out of argv[]. Currently my entry point is this:

int _tmain(int argc, char *argv[]){

	if(argc < 2){
		std::cout << "Drag and drop AZ or OBJ files onto the exe\n";
		std::cin.get();
		return 0;
	}else{
		std::string file = argv[1];
		std::cout << file << "\n";
		std::cin.get();
		import_OBJ(file);
	}

	std::cin.get();

..........

..........

	init();

	glutMainLoop();	

	return 0;
}


Currently my output is simply "E", the drive letter for the file. I assume all the information is there, but I still don't really understand pointers and getting the length of an array. If I loop through the pointer to get chars I'll probably end up with junk from memory.

How can I get the file path?

Is This A Good Question/Topic? 0
  • +

Replies To: Drag and drop files through argv[]

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 06:07 AM

First you don't "Drag and Drop" files through argv[], you run the program with arguments. See this tutorial on Argument passing.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 06:39 AM

Am reading around this and people are pretty much using this same method for handling file input.

Is there a reason why I would only be receiving the first character of the file path?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 07:02 AM

What are you passing to the program as your first argument?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 02:07 PM

I'm literally just dragging a .obj file onto the executable.
Was This Post Helpful? 0
  • +
  • -

#6 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 02:44 PM

_tmain does not exist in C++. main does.

_tmain is a Microsoft extension.

main is, according to the C++ standard, the program's entry point. It has one of these two signatures:

int main();
int main(int argc, char* argv[]);


Microsoft has added a wmain which replaces the second signature with this:

int wmain(int argc, wchar_t* argv[]);


And then, to make it easier to switch between Unicode (UTF-16) and their multibyte character set, they've defined _tmain which, if Unicode is enabled, is compiled as wmain, and otherwise as main.

As for your problem, the first part of the puzzle is that your main function is wrong. wmain should take a wchar_t argument, not char. Since the compiler doesn't enforce this for the main function, you get a program where an array of wchar_t strings are passed to the main function, which interprets them as char strings.

Now, in UTF-16, the character set used by Windows when Unicode is enabled, all the ASCII characters are represented as the pair of bytes \0 followed by the ASCII value.

And since the x86 CPU is little-endian, the order of these bytes are swapped, so that the ASCII value comes first, then followed by a null byte.

And in a char string, how is the string usually terminated? Yep, by a null byte. So your program sees a bunch of strings, each one byte long.

In general, you have three options when doing Windows programming:

Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the -W version of the function. Instead of CreateWindow, call CreateWindowW). And instead of using char use wchar_t, and so on
Explicitly disable Unicode. Call main, and CreateWindowA, and use char for strings.
Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_t.
The same applies to the string types defined by windows.h: LPCTSTR resolves to either LPCSTR or LPCWSTR, and for every other type that includes char or wchar_t, a -T- version always exists which can be used instead.

Note that all of this is Microsoft specific. TCHAR is not a standard C++ type, it is a macro defined in windows.h. wmain and _tmain are also defined by Microsoft only.

Answer found here.
Was This Post Helpful? 1
  • +
  • -

#7 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Drag and drop files through argv[]

Posted 23 April 2011 - 02:47 PM

Wow that is concise. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1