|
Hello! what can I convert a char* to string?
The cast isn't permitted!
Thanks
|
|
|
|
|
public void ConvertStringChar(string stringVal) {
char charVal = 'a';
// A string must be one character long to convert to char.
try {
charVal = System.Convert.ToChar(stringVal);
System.Console.WriteLine("{0} as a char is {1}",
stringVal, charVal);
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string is longer than one character.");
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("The string is null.");
}
// A char to string conversion will always succeed.
stringVal = System.Convert.ToString(charVal);
System.Console.WriteLine("The character as a string is {0}",
stringVal);
}
|
|
|
|
|
char* someCharPtr;
string myString = new string(someCharPtr);
#include "witty_sig.h"
|
|
|
|
|
ok, I converted a char* into a string as:
string descr= new String(obj[1].descr);
but the problem isn't solved:
//obj[] is:
public struct node {
public int IdObj;
public char* descr;
}
node* obj=getObj();
//getObj is the function included into a DLL
//the string "descr" contain only simbols!!!
thanks for your help!
|
|
|
|
|
Why are you using char* in a .NET struct anyway? Stay away from unmanaged types when possible. A char* or char[] is just a string in any language. Even when P/Invoking native functions, you still typically use a string in place of any character array (char* , wchar_t* , TCHAR* , etc.). It will marshal correctly so long as you marshal using the correct character set (see the CharSet property of the MarshalAsAttribute or StructLayoutAttribute if you're doing this to P/Invoke some native function).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If i try to replace the char* to string, the compiler make me the following error:
"Impossible accept address/dimension of variable of managed object."
So the only not managed object are char,int,ecc but not string! (independently of "unsafe" environment)
ps: the language used to build the function into a DLL is the C++, so the "string" is a char[].
|
|
|
|
|
A string is also a char* . The difference is how the character array (string) is allocated.
There are properties you can set using the MarshalAsAttribute that I mentioned earlier) that specify the maximum size (in characters, not bytes) or a parameter index that does the same within a struct (if one exists). Read the documentation for the MarshalAsAttribute in the .NET Framework SDK for more information. Trust me, my forté is unmanaged interop. I've never had to use a single unsafe context to P/Invoke anything from basic to complex structs and function calls. They're really only necessary when you need to boost performance of a block of code; for example, iterating through the bits of a bitmap - using an unsafe context is much faster.
You should also see the Marshal class, which includes methods that further prevent you from having to use an unsafe context (because it does it for you, like alloc'ing memory).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok, I'll read the MarshalAsAttribute documentation!
Thanks very much for help!
|
|
|
|
|
Do I need to know about the encoding of a file before opening it or is it determined automatically by the framework? If .NET framework cannot automatically determine, then is there any way I can find this out myself? I'm not clear about this character encoding thing, so if you can please explain in detail, that would be of great help.
Thx
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
StreamReader has several constructors, some of which take an Encoding and/or a boolean value to indicate whether the encoding should be detected or not.
A bit of poking around in Reflector reveals that if you don't provide an encoding, it uses UTF8, and if you don't say otherwise, it tries to detect the encoding rather than use the default UTF8.
When trying to detect an encoding, it uses the Byte Order Mark character. The Unicode standard indicates that this character, U+FEFF, should appear at the beginning of the text in whatever encoding is used. In UTF-16 little-endian, this becomes the byte sequence 0xFF 0xFE; in UTF-8, it's (IIRC) 0xEF 0xBB 0xBF. If there's no Byte Order Mark, it simply uses the encoding specified in the constructor, unless you didn't use one of those variants, in which case it uses UTF-8. .NET can also detect UTF-16BE, or big-endian, where the bytes of UTF-16 are the other way round.
If you use File.OpenText or FileInfo.OpenText , you don't get to specify an encoding.
Unfortunately very few of us have files encoded as UTF-8. They're far more likely to be encoded using our default code page. For most Western European and North American users, this is going to be Windows 1252 (Windows Western). You can get hold of an encoding for the user's configured ANSI code page using Encoding.Default .
Western users, particularly UK, US and Canada, may not notice at first that the encoding is different, because the first 256 code points of Unicode are the same as ISO Latin 1 (a little, though not a lot, different from 1252). Due to the way it's encoded, the first 128 code points of UTF-8 are also the same as Latin 1 and ASCII (ISO-646-US). Any UTF-8 code byte greater than 127 indicates that one or more following bytes needs to be interpreted along with this one to get the full character.
There's no reliable way to detect which encoding is used by a random sample of text in a byte-oriented character stream (which isn't UTF-8). The concept of Byte Order Marks is relatively new. You either have to know or ask the user.
More information links:
Microsoft Global Development Portal[^]
Code Page reference tables[^]
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
[^]
http://www.unicode.org/[^]
Character Sets[^] (from my blog).
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Great information, thanks Mike.
regards,
Paul Watson
Bluegrass
South Africa
Chris Maunder wrote:
"I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly."
Jon Sagara replied:
"I think we've all been in that situation before."
Crikey! ain't life grand?
|
|
|
|
|
Great information!
I just wanted to add that BOMs (byte order marks) aren't always present in a text file as well. There no requirement for BOMs.
While there's no reliable way to detect encoding - like you said - web browers and other applications (like Word) do try to detect the encoding. If you - the original poster - needs to do something like that, a simple (but probably not the most efficient way) is to take a random sampling of strings within the text file and use StringInfo.GetTextElementEnumerator and enumerate the text elements. With either all of those or a random sample, call TextElementEnumerator.GetTextElement (returns a String ) and check the Length . If it's greater than one, you at least know you're dealing with a multi-byte character set (MBCS), like UTF-8. If all of them were 2 bytes, then it's likely it's a double-byte character set (DBCS), like UTF-16 (there's also 4-byte characters, known as UTF-32!). If they're all 1 byte, then you've probably got an ASCII (or other single-byte encoding) file. From there you can make certain assumptions. You see browsers doing this when they start displaying question marks for chracters in odd places (this also happens when the specified encoding is wrong).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hai there,
i have a collection of label control in my panel control.On button click or based on some events i need to dispose the label controls in Panel (asynchronous ) and need to add few new label controls.
how it is possible /
hai, enjoy coding
Sreejith SS Nair
|
|
|
|
|
You don't need to dispose the labels - just remove them from the Controls collection property of your panel. If they are no longer being referenced (i.e., there's nothing else referencing them like any class fields or something), the GC will pick them up and dispose of them in a (usually) timely fashion.
If you aren't using fields to refer to these labels, then you'll have to iterate (not enumerate) through the Controls collection and find the label controls like so:
for (int i=0; i< Controls.Count; i++)
{
Control c = Controls[i];
if (c is Label) Controls.Remove(c);
i--;
} If you are using fields, you can do something like this:
Controls.Remove(myLabel1);
myLabel1 = new Label();
Controls.Remove(nyLabel2);
myLabel2 = new Label(); Notice that with this way, new label controls are created that the field refer to, meaning that they are no longer refering to the old labels. So long as nothing else is (and they were removed from the Controls collection, so it's not referencing them anymore either), they will be GC'd.
For non-component classes, calling Dispose is important, like instances of the Graphics class you create manually, or an Image or Bitmap . Controls are good about disposing themselves when necessary, but you could still call Dispose if you wanted to, like so:
Controls.Remove(myLabel1);
myLabel1.Dispose();
myLabel1 = new Label();
Controls.Remove(myLabel2);
myLabel2.Dispose();
myLabel2 = new Label(); All this does is free up a little more memory immediately, which would've been freed when necessary by the GC or when the GC got around to it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Do you really need to dispose them, what about just removing? You should be able to use panel.Controls.Remove(controltoremove) for that.
regards,
Paul Watson
Bluegrass
South Africa
Chris Maunder wrote:
"I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly."
Jon Sagara replied:
"I think we've all been in that situation before."
Crikey! ain't life grand?
|
|
|
|
|
I've got a client that wants to send me files automatically to a aspx webpage. Ive got to accept the stream, and save it to the local disk.
Doing manually its easy, but this is automatic, I dont know when the stream will come in or from where. Can anyone help or get me into the right direction with this one?
Is HttpRequest and HttpResponse the right way?
Or TcpListener on the HTTP port?
Will realy appreciate any help, thanks
Dries
|
|
|
|
|
ASP.NET already supports this and wraps it in a nice, neat API for you. All you need to do is include file input controls on your .aspx page (<input type="file" ...> ) inside a <form> . The client portion is done.
For the server portion, see the Files Property[^] of the HttpRequest class, which you can get from Page.Request . This returns you an HttpFileCollection , which contains a collection of HttpPostFile instance. This takes the difficulties out of MIME-multipart that is used to upload files with an HTTP POST.
The documentation for the HttpRequest.Files property does contain an example.
And next time post this in the ASP.NET forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi
i have problem, i have the list of softwares names form the registy. and i am capturing the event opening of any software using wmi, which gives me the .exe name and i use FileVersionInfo to get the software name of that .exe. but this software name doesnt match with the software name from the registry. i have to some how map this software name with the software name from the registry. or i have to find a way to get the software names using an Win32_API, is there an API which will give me all the softwares installed. plz help me in this regard
thanks and regards
deepak
|
|
|
|
|
Why? Do you realize what a performance hit that would be on the machine? Besides, Windows Installer packages are the only installers that really support such functionality, but you don't need WMI to do that. Proprietary installers may not necessary support that (none that I've seen do, and I work a lot with installers and have tried all the major tools).
For Windows Installer packages, you can use the Installer APIs, but this is still a tasking process. There is no way to simply match up a product with a particular file (and a file may belong to more than one product). It is possible, but slow (so I recommend caching).
Why do you want to do such a thing, though? Again, this would seriously hamper the performance of the machine and frankly seems to serve no purpose.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanx for the reply . Well i would like to give you the complete picture of what i am doing.
i have a service that captures the usage of all softwares, ie; how many times an application has been opened by a user in a day. For this i have an inventory of softwares which i picked from the registry. Whenever a user opens a software like say Visual Studio .Net, VISIO etc, i am able to capture this event,ie; i am able to get the exe and the path of the software opened. By using the FileVersionInfo class of .Net i am able to get the software name, but this dosen't match with the name in the software inventory.We know the reason for this, thats because exe is the same for different versions of the same software, for Eg; .net professional edition and Enterprise architect edition have the same exe name(devenv.exe),So it is showing the software name as Microsoft Visualstudio .Net 2003 and not as Microsoft Visualstudio .Net 2003 Enterprise architect-English, which is in the inventory.
I tried with other API's like win32_Product to get the software name, but this gives me only those softwares that are installed using msi installer.This does not help me either.
Is there any other way to get the complete list of softwares that are listed on a computer. Is there any unique identifier for each software that i can use for matching the software names to its right exe.
|
|
|
|
|
You can enumerate the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall, but this will still not help you since it will be full of proprietary installs. Just record the executable name and have a database (even an XML file or something) that matches those executable names with product names. You won't be able to do it any other way. Query all the MSI packages for that filename will be slow (not to mention shared components will belong to multiple packages), and querying proprietary installs would either not work (they'd have to leverage such functionality, and most don't) and would force you to write APIs for all those that do. If that was even possible, it would take an extrodinary amount of time to find which software package the file belongs to; if you use a database of some sorts, this would be much faster.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
once again thanks for the reply
i have exactly used the same registry location to get the software names. are u asking me to record the exe names from that location or create my own database of some sort. but i did try to find the exe names from the same location which u have mentioned but i am not able to find that in that loction.
i was checking with the registry and i found one more location HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths which has a list of exe's. but the problem is that it doesnt give me the full list of softwares.
i am in a terrible pressure, i just need a way out plz tell me if there is any other solution. i am also thinking am i in the right track. is there any other way to think to solve this problem.
thanks and regards
deepak
|
|
|
|
|
What do I have to use or do in C# in order to block the keyboard when, let's say, the timer control has measured a certain period of time?
Thanks for your help!
Best regards,
Cristina
|
|
|
|
|
To block the keyboard for a certain System.Windows.Forms.Form you can use key preview to block all keyboard events:
<br />
private void HandleTimeout(){<br />
this.KeyPreview = true;<br />
}<br />
<br />
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {<br />
e.Handled = true;<br />
}<br />
|
|
|
|
|
Hi!
I'm writing a console application in C# and a dll in unmanaged C++. The dll is loaded via p/invoke and I access some functions in it that are dllexport'ed. What I would like to do is return a string or rather char[] from the dll to the C# console. I have tried several ways but at best I get about half the string, the rest of it is replaced by "?".
I have tried to return a pointer to the string. This is when I get the "half string". ( the secont half of the string actually.. ?? The first ten letters are questionmarks.. )
What I would like to do is to pass a char* as an argument and that it should be pointed to a string in the unmanaged code. Is it possible? What should I pass as argument and what should I recieve at the dll?
|
|
|
|
|