Skip to main content
GameDev.net gamedev.net
🔒 Locked

Passing a file from extension to command args when running a program?

Started by KazenoZ Jul 16, 2011 at 3:37 PM 10 replies 3.1k views
Original Post
KazenoZ
KazenoZ
Hello,

A pretty raggedy question, I couldn't really find exact answers to...

So, I want to make a file extension for a file type I'm using with my program, and I want that after the extension is set, and the file with that extension is double clicked, I want it to feed the file's path as a startup command argument in the program, so, how do I do it?

Currently, I have this:

exeFolder = Environment::CurrentDirectory;
String^ keyName = "HKEY_CURRENT_USER\\Software\\Classes";
String^ subKey = \\SilverBridge.Phantasya;
//Registring the file extension if not already registered
String^ reg = Convert::ToString(Registry::GetValue(keyName+subKey+\\shell\\open\\command, "", "-1"));
if(reg == ""){
Registry::SetValue(keyName+subKey+"\\shell\\open\\command", "", exeFolder+"\\CLRTest.exe");
Registry::SetValue(keyName+"\\.gpj", "", subKey);
Registry::SetValue(keyName+subKey+"\\DefaultIcon", "", exeFolder+"\\CLRTest.exe");
}


So... any good programmatic way?
Thanks!
SiCrane
SiCrane
If I understand the question, in the command line a %1 will expand to the file name that is being invoked.

Also, did you stick all those funky bracketed url strings in your code, or is that the forum software going crazy?
KazenoZ
KazenoZ
Yes, sorry, it was the forum acting up, didn't let me write a double backslash without making it a URL, sorry about that =\

And I'm sorry that I wasn't very clear, I was rushed to errands, so I had to write this a little quickly.
What I meant, is that, yes, that is the programmatic way to set the values for the first time, so now each .gpj file on my system should be automatically opened with the program I set it to open with here, and have the same icon as that program, right?
But when I use this method to open a .gpj file with my program, the program would just crash, it, not only doesn't pass its' path as an argument, it makes the program not run, and I'm not quite sure why, as I can't debug when opening it by other means than the compiler...


I'm sorry if I'm still not very clear. =\
ApochPiQ
ApochPiQ
Can you post what the generated registry entry looks like? Unfortunately I can't make heads or tails of it from the code since the forum beat the crap out of it...
KazenoZ
KazenoZ
Sure, there you go.

As far as I can tell by looking at other applications, this seems to have worked smoothly enough though.
SiCrane
SiCrane
As I said, you can use %1 in the command entry, which will expand into the file name. In general you wrap it in quotation marks so that it will be parsed as a single argument.
KazenoZ
KazenoZ
No, I don't think that's it though...

Things is, this is my main function:

int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
if(args->Length > 0)
Application::Run(gcnew Form1(args[0]));
else
Application::Run(gcnew Form1());
return 0;
}

Basicly, it's a Managed C++ form project, so I check if there are any arguments passed to the process when it starts, and if there are, I forward the path to an overloaded constructor for the main form of the project.
This works well when I put the path in the command arguments in Options->Debugging(VS2008) and debug from inside the compiler, but when I open the file using the program from outside the compiler it just crashes with the general "send/don't send error reports to Microsoft" error, so I don't think it's about parsing the argument.... Or am I getting it wrong?

fredrik@fredrikak.net
fredrik@fredrikak.net
Try Application::Run(gcnew Form1(args[1])); instead. The first arguments is the filename of the executable, while the later are the passed arguments
KazenoZ
KazenoZ
No, that part is actually right...
I know it's true in Native C++ that the first real argument is 1 and not 0, but apparently it's not the same in MC++.
Since when I change if(args->Length > 0) to if(args->Length > 1) and open the file with the program it'll go into the else case and use the default constructor, and since when I add the path in the command arguments field in the options it only has 1 argument still.
So this implies that there's only 1 argument passed, and that it is the one I need, but for some reason it just crashes the program, and I can't seem to get why...

fredrik@fredrikak.net
fredrik@fredrikak.net
Have you tried to compile it and print the filename to some type of logging system or stdout, before creating the form? See if the path is correct and complete
KazenoZ
KazenoZ
Wow, yea ><
That's it, thanks alot. The error wasn't with passing the path, it was somewhere later in the code that didn't work with this path.

Thanks =\

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.