|
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
|
|
|
|
|
How to get the time when the program executing
Mr Duc Linh Nguyen
|
|
|
|
|
Do you mean DateTime.Now ?
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
I mean. I got a function and i execute it, then i want to know how long does function execute.
Mr Duc Linh Nguyen
|
|
|
|
|
Just record the time when the application/method begins execution and diff the time when it stops.
DateTime start = DateTime.Now;
TimeSpan duration = DateTime.Now - start;
Console.WriteLine("Ran for {0} milliseconds.", duration.Milliseconds); This is basically what Christian's source that someone mentioned is doing. I mean, how else would you do it?
If you want a more thorough solution, I recommend a CLR profile like this free one[^] from Microsoft.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Dear all,
i try to popup a dialog when i change item in a drop down list. i use javascript:
SelectDropDownList1.Attributes.Add("SelectedIndexChanged","Javascript: return confirm('sure?');");
the dialog won't display.
if i use:
SelectDropDownList1.Attributes.Add("onchange","Javascript: return myconfirm();");
confirm dialog will display, but it won't fire my function in code behind of SelectDropDownList1_SelectedIndexChanged.
would anyone give me some advice on this hassle? thank you.
David
|
|
|
|
|
Is AutoPostBack set to true on the control ?
Try not! Do or do not, there is no try. - Master Yoda
|
|
|
|
|
First of all, it's "javascript:return myconfirm();" (notice the colon), but you shouldn't use the "javascript" declaration. This scheme is only necessary when you use it in an attribute that accepts a URL, such as the A.href attribute.
If you change the the handler for the onchange event of your element, auto-post back won't occur since events only work with a single handler. You can, however, call __doPostBack (with the necessary params) in your myconfirm function. The work-around for this problem is just what I mentioned: since you can only have a single handler for an event, have that handler call other functions. Of course, you should call __doPostBack last since it will create a new request (thus resulting in round trip to the server).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|