Open with... command

closed account (D9yvqMoL)
I am wondering how to use Open With... to copy path, i used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>;
using namespace std;

int _tmain(int argc, _TCHAR *argv[])
{
	if(!argv[1]){
		std::cout<<"No Variables.\r\n";
		system("PAUSE");
		return 0;
		std::exit;
	}
	std::cout<<"Path: "<<argv[1]<<"\r\n";
	system("PAUSE");
	return 0;
}

That say No variables, Or Path: 30006

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>;
using namespace std;

int _tmain(int argc, char *argv[])
{
	if(!argv[1]){
		std::cout<<"No Variables.\r\n";
		system("PAUSE");
		return 0;
		std::exit;
	}
	std::cout<<"Path: "<<argv[1]<<"\r\n";
	system("PAUSE");
	return 0;
}

Output is No variables or Path: C

Thanks in forward.
Last edited on
You'll want to test against 'argc' to if any arguments were passed to main, otherwise you run the risk of trying to access an invalid address.

To display wide strings you want to use "std::wcout". This means that your string literals have to be prepended with a capitol 'L'. So this:
 
std::cout<<"Path: "<<argv[1]<<"\r\n";

Becomes this:
 
std::wcout<< L"Path: "<<argv[1]<< L"\r\n";

You'll need to make similar changes all around.
closed account (D9yvqMoL)
Dear @Computergeek01

I am making Runtime Evernoment whish will run: system("java -jar mylang.jar " << argv[1]; where argv[1] represents path, and run it.

That will run JAVA command, and run evernoment. Thanks for this.

P.S.
How to add specific thing to PATH using C++ console app?
Take the string literal data that you are trying to prepend to the path and make it into an std::string. From there you have your choice of the "+=" operator or the "std::string.append()" function to add the path passed in to your string. Then, if you insist on using the "system()" function, you'll need to use the "std::string.c_str()" member function to pass the result on.
system("java -jar mylang.jar " << argv[1]; That's a pretty big security hole. Someone could inject any code they wanted into that.
closed account (D9yvqMoL)
Ok, @giblit is there alternative?

@Computergeek01 is it gong to be C://myfile.mylang, or just C, like with cout, with char *argv[] or, _TCHAR *argv[]

P.S.
It is _TCHAR. Thanks. and java thing??

P.S. 2
And PATH thing??
Last edited on
ArsenArsen wrote:
Ok, @giblit is there alternative?
I believe it is the same as using the CreateProcess method since you are on windows. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

_TCHAR is a wide string when the unicode macro is enabled otherwise it should be a narrow string IIRC.
The string that you receive from argv[1] will be what ever you pass to it. I guess I don't understand the question.
closed account (D9yvqMoL)
@Computergeek append dont work, can u give me example code?

@giblit how to run that java program? Since java is under path.
1
2
3
4
std::string Command = "java -jar mylang.jar ";
Command.append(argv[1]);

//THEN LAUNCH YOUR CODE 


This is something that a batch file could do. Is this academic?

EDIT: Don't forget to include the 'string' header file. That might be why it's not working for you.
Last edited on
closed account (D9yvqMoL)
error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::append(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from '_TCHAR *' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> Reason: cannot convert from '_TCHAR *' to 'const std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

and, its at Command., at dot. And too few arguements.


Included firstly string, then string.h

Also, it needs argv[1] then some int by constructor.
Last edited on
Sorry, I assumed that you read giblet's post. _TCHAR is a wide character string. If you are using wide characters, then you need wide strings to accommodate them.

This: std::string Command = "java -jar mylang.jar ";
Should become this: std::wstring Command = L"java -jar mylang.jar ";

Take note, the "system()" function does not take wide strings as an argument.
Last edited on
closed account (D9yvqMoL)
Oooooo, thats ok, work, is it adding path to end, and CreateProcess

I didnt understanded you good at start,
closed account (D9yvqMoL)
What to do with CreateProcess, @giblit
closed account (D9yvqMoL)
Sorry for annoyingness, but i need example, i dont understand it, little newer to c++, but i wanna my RE as first project at C++
Topic archived. No new replies allowed.