|
I have a legacy DLL written in unmanaged C++ that I need to use in a C# application. I can use it ok, but testing shows we don't get the string information when we catch any exceptions it throws.
It throws a simple TCHAR string exception. example:
throw(_T("My Exception Message."))
We can catch it in C# with the System.Exception class, but the string message is lost. (The Message member is empty.) We tried using a RuntimeWrappedException but that did not catch it.
I can not change the code in the DLL. How can I catch these exceptions and get the string info they contain?
|
|
|
|
|
Not Knuth wrote: I can not change the code in the DLL.
But you can create a C++/CLI assembly that can 1) catch the native exception, 2) Create a managed exception and marshal the message into it 3) throw the managed exception.
|
|
|
|
|
True. And thanks for your suggestion. But I hope there is a more direct way to accomplish the same thing. Surely the designers of the framework made some provision to catch unmanged exceptions AND get the data they may contain. (Unlike the RuntimeWrappedException.)
|
|
|
|
|
Not Knuth wrote: But I hope there is a more direct way to accomplish the same thing.
I prefer to count on C++ rather than hope, but proceed as you like.
|
|
|
|
|
I prefer to use the best method available. I am not yet convinced that yours is the best method. It will work, but it is very inefficient.
|
|
|
|
|
Not Knuth wrote: but it is very inefficient.
Compared to what? It is currently your only known solution so it is the most efficient solution.
|
|
|
|
|
Following that logic, it is also the most inefficient solution!
It might turn out to be the best option. But perhaps there is another one that neither of us know. That is why I posted my question here.
|
|
|
|
|
What does the innerexception property show?
I have no blog...
|
|
|
|
|
Liam O`Hagan wrote: What does the innerexception property show?
The inner exception is null.
|
|
|
|
|
Not Knuth wrote: I can not change the code in the DLL. How can I catch these (C++) exceptions and get the string info they contain?
I have recently encountered a similar situation. After a good bit of research and testing I can tell you that the best approach is to wrap the DLL in a managed assembly where you catch the C++ exception and then throw a managed exception in its place. (As described previously in the reply from led mike.)
You can grab the string out of the C++ exception and insert it in one of the exception classes defined in the framework or in your own derived from one of them. The framework doesn't provide any method to automatically marshal the string message in a C++ exception to a managed object.
Marshall
If you continue to do the same things you always did, don't be surprised if you get the same results you always got.
|
|
|
|
|
|
Hi, I'm writing a MediaPlayer so I need to keep the monitor on and screen saver disabled during the life of my application. I understand that I need to handle certain windows messages SC_SCREENSAVE, SC_MONITORPOWER but I'm not sure how. So Please Advice
Thanks...
|
|
|
|
|
|
I got the following suggestion from someone
START
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SYSCOMMAND:
switch (wParam)
{
// disable screen saver and monitor power-saving mode
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
END
I goggled and tried to find various Constants used and replaced them with their values
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
//case WM_SYSCOMMAND: //REPLACED
case 0x0112:
switch (m.WParam.ToInt32())
{
// disable screen saver and monitor power-saving mode
//case SC_SCREENSAVE: //REPLACED
case 0xF140:
//case SC_MONITORPOWER: //REPLACED
case 0xF170:
return;
}
break;
//case WM_DESTROY: //REPLACED
case 0003:
PostQuitMessage(0); // Wont Compile in C#
return;
}
base.DefWndProc(ref m);
//return DefWindowProc(hwnd, message, wParam, lParam);
}
NOw the Function PostQuitMessage(0) wont compile and without my whole Control layout goes wrong
I checked the link you provided but being very new to Windows Programming & C# I dont know how to use all this.
So your thoughts...
Thanks...
|
|
|
|
|
Include the following in your code:
[DllImport("user32.dll")]
public external void PostQuitMessage(int returnVal);
Jeff
|
|
|
|
|
After Updating the code and running my observations are that my MainForm does not show anymore and the app just exits (Runs fine after removing the following bit of code).
[System.Runtime.InteropServices.DllImport("user32.dll")]<br />
public static extern void PostQuitMessage(int returnVal);<br />
<br />
protected override void WndProc(ref Message m)<br />
{<br />
switch (m.Msg)<br />
{<br />
case 0x0112:<br />
<br />
switch (m.WParam.ToInt32())<br />
{<br />
case 0xF140:<br />
case 0xF170:<br />
<br />
return;<br />
}<br />
<br />
break;<br />
<br />
case 0003:<br />
PostQuitMessage(0);<br />
return;<br />
}<br />
<br />
base.DefWndProc(ref m);<br />
}
|
|
|
|
|
Put in some breakpoints and find the path that the code is taking to exit your application. Then post the results here so we can get a better idea of what is going on. Why do you call base.DefWndProc instead of base.WndProc?
Jeff
|
|
|
|
|
Sukhjinder_K wrote: being very new to Windows Programming
Sukhjinder_K wrote: I'm writing a MediaPlayer
very new and writing a MediaPlayer
Sukhjinder_K wrote: So your thoughts...
My thought is, that does not compute.
|
|
|
|
|
I have programmed in Java a lot. So I'm just new to Windows Programming and C# and have never done Windows Message handling (or anything having HWND in it) before. I was looking for a Free Media Player which had what I needed, I couldn't find one. So I decided to build one on my own. And I chose C# to learn a new Language/Platform.
Thanks for your thoughts Mike...
|
|
|
|
|
Did you check it switching to appropriate Case block?
|
|
|
|
|
Thanks Everybody for your help. Here is the Solution
const int WM_SYSCOMMAND = 0x0112, SC_SCREENSAVE = 0xF140, SC_MONITORPOWER = 0xF170;<br />
protected override void WndProc(ref Message m)<br />
{<br />
if( m.Msg == WM_SYSCOMMAND )
{<br />
if( m.WParam.ToInt32() == SC_SCREENSAVE || m.WParam.ToInt32() == SC_MONITORPOWER )<br />
{
return;<br />
}<br />
} <br />
base.WndProc(ref m);<br />
}
|
|
|
|
|
Hi All,
I am writting a C# project in which am using Browser Control. The object of this project to read values of JavaScript's variables values on the current page ,and add those values to ListBox control. Can anybody show me how to do this. Please help if you can I need to do this ASAP for my job.
Thanks,
Asif
|
|
|
|
|
A.Asif wrote: am using Browser Control.
There are many articles on MSDN and I imagine some on CodeProject as well for using the Browser Control in C#, try starting there.
|
|
|
|
|
I did research almost erevry site including microsoft but found no luck to my perticullar problem.
So please help if you can
Thanks,
Asif
|
|
|
|
|
Add a reference to the MSHTML.dll and cast to the DOM types as explained in some document you read (you said you did them all). Then simply use the DOM to get the values.
xacc.ide
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."
|
|
|
|