Strings and Escape Characters

G

Guest

I am storing a printer name in the registry. If its a networked printer, it
stores as so: \\server\printername.
When I pull that value in code, and throw it in a string, I get
\\\\server\\printername.

I get why this is happening, but I have a legacy dll that I need to pass
that value into. It does not like me passing the value that way. However,
if I hardcode the value \\Server\Printername and pass that in, its just fine
with it. I am unsure what language the dll is written in. Is there any way
to prevent C# from tacking on the escape sequences on the \'s ?
 
R

Randolph Potter

Try prefixing the string with the @ sign, e.g. string PrinterName =
@"\\server\printer" ... It might help. Without seeing your code though, it's
difficult to say.

R
 
G

Guest

To clarify a bit. I need the string to hold the actual value:
"\\Server\Printername". Is there a way to put a sing \ into a string?
 
N

Nicholas Paldino [.NET/C# MVP]

Rahvyn,

The way that you see it in the debugger is just the interpretation of
the string, the string still has only two slashes before the server name,
and one slash to delimit the printer name. There isn't an actual extra
slash in your string, unless you are adding it yourself.

Hope this helps.
 
G

Guest

Well, I can send the value to the dll just fine, using the @ symbol. So this
call:

PrintDoc(@"\\Server\Printername", Reply);

works just fine whereas this one:

printer = regKey.GetValue("Printer").ToString();
PrintDoc(printer, Reply);

Does not work.
 
G

Guest

Nicholas;

I understand its just showing like that in the debugger. But for whatever
reason, when I try to pass to the function with a string variable, it fails,
but with a hardcoded value of @"\\Server\PrinterName".

This is the function declaration in the includes file for the dll:
int PrintThermalWithNamedPrinter(char *PrinterName, char *RequestBuffer,
char *ReplyBuffer, char *SavePath);

Would it make a difference to pass a character array? Will C accept that?

Nicholas Paldino said:
Rahvyn,

The way that you see it in the debugger is just the interpretation of
the string, the string still has only two slashes before the server name,
and one slash to delimit the printer name. There isn't an actual extra
slash in your string, unless you are adding it yourself.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rahvyn said:
I am storing a printer name in the registry. If its a networked printer,
it
stores as so: \\server\printername.
When I pull that value in code, and throw it in a string, I get
\\\\server\\printername.

I get why this is happening, but I have a legacy dll that I need to pass
that value into. It does not like me passing the value that way.
However,
if I hardcode the value \\Server\Printername and pass that in, its just
fine
with it. I am unsure what language the dll is written in. Is there any
way
to prevent C# from tacking on the escape sequences on the \'s ?
 
N

Nicholas Paldino [.NET/C# MVP]

Rahvyn,

Without seeing how you are declaring it for calling through the P/Invoke
layer, the signature in the header file will only give 1/2 of the needed
information.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rahvyn said:
Nicholas;

I understand its just showing like that in the debugger. But for whatever
reason, when I try to pass to the function with a string variable, it
fails,
but with a hardcoded value of @"\\Server\PrinterName".

This is the function declaration in the includes file for the dll:
int PrintThermalWithNamedPrinter(char *PrinterName, char *RequestBuffer,
char *ReplyBuffer, char *SavePath);

Would it make a difference to pass a character array? Will C accept that?

Nicholas Paldino said:
Rahvyn,

The way that you see it in the debugger is just the interpretation of
the string, the string still has only two slashes before the server name,
and one slash to delimit the printer name. There isn't an actual extra
slash in your string, unless you are adding it yourself.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rahvyn said:
I am storing a printer name in the registry. If its a networked
printer,
it
stores as so: \\server\printername.
When I pull that value in code, and throw it in a string, I get
\\\\server\\printername.

I get why this is happening, but I have a legacy dll that I need to
pass
that value into. It does not like me passing the value that way.
However,
if I hardcode the value \\Server\Printername and pass that in, its
just
fine
with it. I am unsure what language the dll is written in. Is there
any
way
to prevent C# from tacking on the escape sequences on the \'s ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top