|
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?
|
|
|
|
|
If your unmanaged declaration looked like this:
LPCTSTR SomeFunc(); Then your managed declaration should look like this
[DllImport("whatever.dll", CharSet=CharSet.Auto)]
private static extern string SomeFunc(); Notice the CharSet=CharSet.Auto ? That marshals the string correctly. Most likely what you're seeing is the result of marshaling an ASCII string as Unicode, or a Unicode string as ASCII. Strings in .NET are all treated as Unicode, so you have to tell the CLR how to marshal strings from unmanaged code. See the CharSet enumeration in the .NET Framework SDK for more information.
The reason some characters are written as ? is because the character representation isn't printable, meaning you got some junk data in there. This could be an indication that something is wrong with your unmanaged function.
You should read Consuming Unmanaged DLL Functions[^] and Interop Marshaling[^] in the .NET Framework SDK. What you're trying to do is easy, but you need to understand the basics first.
BTW, char* or char[] is a string (in this case, an LPSTR, as defined in the Windows headers, but it could be define as anything). When P/Invoking unmanaged APIs, it's better to use a string and marshaling it (also see the MarshalAsAttribute for more assistance and information) rather than a char array. It works the same, but passing a string is much easier than passing a char array.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi!
Thanks!
But I still get the wrong output.
My exported dll has a function that first writes the string to export to file and then returns it,
Ie: __declspec(dllexport) LPCTSTR GetString( int i )
{
FILE* file;
file = fopen("test.txt", "a" );
fprintf(file, "%s%c",m_strings[i], '\n');
fclose(file);
return m_devices[i];
}
This gives the correct output in the file, but the string I receive in the managed console is containing ten questionmarks first and then some correct charakters:
---
test.txt:
PCI-1750 I/O=2040H
Advantech DEMO I/O=1H
---
Console output:
????????? /O=2040H
??????????HEMO I/O=1H
This is really confusing...
|
|
|
|
|
And what does your managed declaration look like?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
[System.Runtime.InteropServices.DllImport("pciDriver.dll", CharSet=CharSet.Auto) ]
private static extern string GetString( int i );
and I call it like:
GetNumberOfStrings( ref i );
System.Console.Out.WriteLine( GetString( j ) );
|
|
|
|
|
That's correct, so the problem is in your unmanaged code. How is m_Devices declared? What codepage is Windows set up for? Are you using a different codepage for your Console ? Also, since your using just printf and not tprintf , this tells me you're not using ASCII on Windows and Unicode on Windows NT (like Auto dictates), so use CharSet.Ansi instead (if this is the case). Like I said before, you should read the documentation, not just expect the exact answer (especially when you don't provide enough details to say for sure).
One thing I would warn you about as well is that it's actually a bad idea to return strings from functions. I don't remember all the details (it's been a while since I was hot n' heavy into C/C++), but you should instead declare your function like so:
__declspec(dllexport) void GetString(INT i, LPSTR lpszText, DWORD cbText); Also notice my use of INT (declared as int , and it's typically better to use pre-proc defs). The lpszText param is both [in,out], and the cbText ([in]) is the max string length. Without this protection, you open your code up - and potentially your entire system - to buffer overruns (the leading cause of security threats). This signature is common for functions and structs throughout the Windows APIs.
Your managed declaration would then look like this (taking into account that is seems you're using ASCII only strings since you're using
printf</code):<pre>[DllImport("pciDriver.dll", CharSet=CharSet.Ansi)]<br />
private static extern void GetString(<br />
int i,<br />
[In, Out] string lpszText,<br />
[MarshalAs(UnmanagedType.U4)] int cbSize);</pre>Technically, however, the first param - <codE>i - should be an IntPtr . An int in C# refers to Int32 , which is always 32 bits. An int in C/C++ is the width of the processor, so 32 bits on a 32-bit processor and 64 bits on a 64-bit processor (depends on the compiler, really). An IntPtr is like that. If you wanted to make your code more portable to other architectures, then change the param type to IntPtr and when you pass it, pass it as new IntPtr(0) (or whatever index you need).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi friends,
I have one C++ dll library which allows to pass in a LOGFONT structure. How can I convert C# Font to LOGFONT ? I know we can use Font.ToLogFont(), but I don't know how to use this function.
Can anyone give me some ideas?
One more question, can I directly create a cFont from LOGFONT in C++?
actually, I hope to convert Font(c#) to cFont directly.
Thanks for help.
|
|
|
|
|
azusakt wrote:
can I directly create a cFont from LOGFONT in C++?
CreateFontIndirect would be the method for that, its a member of CFont.
azusakt wrote:
How can I convert C# Font to LOGFONT ? I know we can use Font.ToLogFont(), but I don't know how to use this function.
There could be other ways, but you can declare a structure similar to the LOGFONT in c# and then fill in this structure with the values from Font object and pass this structure to your C++ library. You need to go through P/Invoke to achieve this.
Your LOGFONT structure in C# would look like this,
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string lfFaceName;
}
Update :
You'd need to create an object of tihs type and then call ToLogFont function.
Hope this helps.
|
|
|
|
|
Actually, you don't even need to create the structure - only alloc enough memory to hold the LOGFONT information (remember that C/C++ has no such CTS like .NET), taking into account those last 32 characters, which could either be 32 bytes or 64 bytes depending on the current platform. Either way, it doesn't matter.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for all of your suggestions.
I just got an idea to create some variables for storing up the fonts status only, like bold, Italic etc..
Because I can't sure all variables in LOGFONT are useful, since Fonts(c#) does not contains lfEscapement, IfOrientation, lfOutPrecision, IfClipPrecision etc.
So I concern it may not able to output a correct text with the font I set in C# Fonts. I don't know what if I select some Asian, France, or Symbol for outputing.
so, what is the best way to do??
|
|
|
|
|
As the other person mentioned, you just call Font.ToLogFont - it takes care of everything. You don't need to fill the struct (or memory block) yourself. The LOGFONT does include all the parameters necessary to display your text using the Font you specify. To make sure that other fields are filled in, also pass an instance of the Graphics object for your operation, which you would get either from the PaintEventArgs (if in a Paint event handler) or from a call to CreateGraphics (don't forget to dispose the Graphics object in the latter case - do not dispose of it in the Paint event handler since the calling code will do it).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
One More problem is the IfFaceName is TChar, how can I make it possible to store unicode FontName, like Chinese Font Name / Korean Font Name?
|
|
|
|
|
I'm sorry, I just got one problem can't solve yesterday....
If the font name I selected in C# is a Chinese name or Korea name, it can't fill correctly in C++. C++ just got one character.
I think it is the Unicode problem. When I select the font with Chinese name or Korea name, it can't copied to IfFaceName(in C++), because IfFaceName is TChar.
So I tried to set the project to use Unicode CharSet, then all chars become WChar; should I re-define the Size of IfFaceName, as it only allows max 32 chars. And also I can't use strcpy to copy WChar to Char ??
How can I prevent this problem ??
Thanks for help
|
|
|
|
|
Hi gurus,
In C#, I am writting a small program that sends text over a network (lan/web) using the sockets.
I can connect a client to a server, but I how can I see if a client disconnect from a server after being connected and how a client can see if the server is disconnected after being connected?
Thanks.
There is no spoon.
|
|
|
|
|
Try communicating in a set interval (a "ping" as it's commonly called). If they aren't connected for any reason, and exception would be thrown so be sure to handle it. There really is not other sure way of doing. Sure, you could implement some mechanism in your protocol where the client tells the server it's disconnecting, but that would assume that the connection wasn't interrupted. You could still implement such a protocol (it's common, like in SMTP, FTP, DICT, etc.), but don't rely on that as the only way for the reason I mentioned previously.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am using quartz.dll in a thread to play some background music. I have made the sound named mc and i call mc.Run() inside a thread that i ahve named backgroundsound.
In any case when i close the application It still shows up in the processes as running. I can't seem to figure out how to complete dispose of all mc objects or threads if you will.
In my dispose i call backgroundsound.abort() to abort my thread and i call mc.stop() to stop the playing of my sound. Nothing seems to work any suggestions would be great.
Win32newb
"Programming is like sex, make one mistake and you have to support it for a long time"
|
|
|
|
|
Aborting your thread is not a good idea. You should terminate it gracefully when possible, whether that's through some static field you poll or some other mechanism.
The solution is more simple, though. When you create your thread, set Thread.IsBackground to true before you call Start . This won't prevent the thread from executing like a foreground thread, which instance of the Thread class are by default.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
I am experimenting with ASP.NET charting. I have a bitmap of a given length and width and what I want to do is draw a string in the center of it. Is there a way to calculate the string parameters according to the specified font etc., so that I can know where exactly to draw it so that it will fall in the center.
Thanks,
Pankaj
Without struggle, there is no progress
|
|
|
|
|
You can try the MeasureString function, it gives you the size occupied by the string for the specified Font object.
|
|
|
|
|
Thanks Kannan!
I was using font.Size methods and all and wondering why it was not working.
Sincerely,
Pankaj
Without struggle, there is no progress
|
|
|
|
|