Compiler Thinks Backslashes Are Escape Sequences

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 3060 Views - Last Post: 31 March 2017 - 08:28 AM

#16 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 04:56 PM

		String^ serverip = textBox1->Text;
		string serveripstd = marshal_as<string>(serverip);
		string myIpVar = "\\\\" + serveripstd;
		string fullScriptOut;
		String^ destination = textBox2->Text;
		string destinationstd = marshal_as<string>(destination);



		string scriptOut;

		if (checkBox1->Checked == true)
		{
			scriptOut = myIpVar + "\\" + "xxxxxInstall\\CurrentMSI -Recurse";
			fullScriptOut = "Copy-Item" + scriptOut + "\\\\" + destinationstd + "\\";
		}
		
		
		ofstream a_file("move.ps1");
		
		a_file << fullScriptOut;
		
		a_file.close();




Spaces are missing but that's just a tweak...

Anyone know how to run .ps1 scripts straight out of C++/.NET?
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 06:24 PM

Actually, PowerShell will accept forward slashes. I mix and match them all the time to the chagrin if people who like to steal my scripts.

Check MSDN. It has a section about how to host PowerShell.

I am still quite concerned that you are pushing off to PowerShell something that you could do by simply calling the appropriate Win32 API.
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 24 March 2017 - 05:59 AM

Moving this to C++/CLI since the code in post #16 is in that language...
Was This Post Helpful? 1
  • +
  • -

#19 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 27 March 2017 - 03:21 PM

View Postr.stiltskin, on 22 March 2017 - 01:45 PM, said:

No matter. The same principle applies in C++. Both \ and " are special characters so you have to use an escape sequence to print them. So \\ prints a backslash, \" prints a double quote.

But I just realized that you probably didn't want to print that " mark, so what I wrote earlier is wrong. It should be \\\\Hostname\\, so in C++ if you write something like
string s = "\\\\Hostname\\"
cout << s << endl;

I think you'll get what you wanted.

But if you did want the quotation marks just add \" before the first backslash and after the last one.


Nice!! hey this really helped me today.

( the part about \" )

View PostSkydiver, on 23 March 2017 - 06:24 PM, said:

I am still quite concerned that you are pushing off to PowerShell something that you could do by simply calling the appropriate Win32 API.


I'll have to check that out... I didn't know you could just use a win32 API to move stuff around. It isn't too bad though because at least I'll know PS better at the end of this.

I've almost got this PS/C++/.NET mover feature fully functional... might even be beta before I leave work today.
Was This Post Helpful? 0
  • +
  • -

#20 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 09:40 AM

Anyone know how to execute a PS script using CLI/C++?

Oh and also, I've heard a lot of banter about not using the system() commands due to security reasons... can/should I use system(something) for this? or something else? I'm thinking a general shell execute command should suffice... ideas?
Was This Post Helpful? 0
  • +
  • -

#21 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,481
  • Joined: 12-June 08

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 09:42 AM

How much do you care about security?

I figure pop up using "start powershell.exe Set-ExecutionPolicy RemoteSigned" then tell it to run your power shell script.
Was This Post Helpful? 1
  • +
  • -

#22 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 10:27 AM

system("start powershell.exe");


worked... now just need to know how to run a script using parameters.

So basically will just keep that then use conditionals to control which scripts are run.
Was This Post Helpful? 0
  • +
  • -

#23 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 10:56 AM

Does anyone know how to execute a powershell script using system("start powershell.exe") ???

I'm guessing there's some args I can throw in but I don't know the syntax and google doesn't really have any good answers for CLI/C++ .. plenty for C# but not C++. I'd like to avoid using a .bat file if possible... would be sloppy. Can I just set .ps1 files to exec with PS and then somehow use a shell invocation?

As always, I'll post my solution if I find one.

Simpler the better.

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#24 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,481
  • Joined: 12-June 08

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 10:57 AM

A cusory search picked up typically after starting the powershell with execution policy then you would use something like

system("start powershell.exe <drive letter>:\\<powershell file name>");

Was This Post Helpful? 1
  • +
  • -

#25 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 11:20 AM

yeah I tried that and it didn't seem to work. I found the same thing

I'm not sure what the problem was, though, because the PS window flashed so fast I couldn't read the text. It looked red haha :wheelchair:
Was This Post Helpful? 0
  • +
  • -

#26 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,481
  • Joined: 12-June 08

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 11:32 AM

You are using Visual Studios, right? Why not slap a break point in there and step through it all.
Was This Post Helpful? 1
  • +
  • -

#27 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 11:51 AM

OH and PS... sorry that I posted another topic... really was so that in the future someone else with the same problem could easily find the solution to that problem. But I understand why it got deleted, my bad.

ok yeah, just tried the BP ... never used one before so I think I messed it up. What I should probably do is maybe just throw a stop command into the PS script?

Ahh I think I know what the problem is.. I need to run Powershell as an administrator due to GP at work here... is there an arg for that?
Was This Post Helpful? 0
  • +
  • -

#28 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,481
  • Joined: 12-June 08

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 11:54 AM

Break point info found in the Debugging tutorial.

As for the admin - I figured that's what the 'Set-ExecutionPolicy' was for.
Was This Post Helpful? 1
  • +
  • -

#29 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 01:09 PM

well it won't let you Set-ExecutionPolicy unless you're running as admin. Tis ok though, I can probably find the arg for run as admin on google. Thanks Modi
Was This Post Helpful? 0
  • +
  • -

#30 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 28 March 2017 - 01:44 PM

So this is starting to really discourage me... does anyone know how to simply run a file as if I double clicked on it? Is there a command in .NET/C++ to run a file as if you had run it through the shell?

iFeel like there's gotta be an easy way to do this and I've already come this far so giving up now seems pointless.

I wish I could see what powershell was telling me but the breakpoint doesn't seem to make a difference.. it does stop the program, but powershell still flashes for a split second.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3