I am logging error messages and .NET exceptions of my application in a database and display them on a GUI. The GUI displays error messages in the language of the locale of the user running the application. In order to achieve this I log resource names of error messages only, and turn them into meaningful messages using resource files.
I, however, cannot wrap my head around how to achieve the same for .NET standard exception messages. I can only log the message string, but then the language is already 'baked in' so I can only display the exception in that single language.
Is there a way to get the resource name of the .NET exception + all parameters that are inserted into the message string, so that I can log these?
There is no such thing as resource name for an exception, but there is the HResult property of the Exception class. The standard .NET framework exceptions have valid and unique HResult numbers which you can map then to your own resource identifier that you can translate then.
Furthermore there is the Exception.Data property as well, which may contain additional information about the exception.
Or, you can parse the exception messages manually, using regex or such and then map to your resource id, but this is too much effort I think.
I had this problem too and did a fair amount of research on it. Based on everything I could tell there was not a way to localize the exception itself because what you're seeing is not localized strings, it is actual code. The code is only written in one language (english) and can only be displayed as such.
I have a DLL file with four functions, I need to get access to the four functions but they don't have names, just entry points which I know. I have to figure out what the four functions do.
The question is, how do I call these unknown functions in C#(.Net)? And how can I figure out what their parameters and return values are?
The DLL is Sndvolsso.DLL . It's functions are nowhere on the internet.(unless you can find them) If anyone knows of a tool I can use to read parameters and return values, that would be very helpful.
If your DLL is unmanged:-
Using IDA
Disassemble the dll and find the exported functions.
Double click that function and its declaration will be shown in IDA.
An Example of MessageBoxA of user32.dll in IDA:-
.text:77D6EA11
.text:77D6EA11 ; int __stdcall MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
.text:77D6EA11
You can also use the free version of IDA 5.0.
Parameter / type / signature information is not stored in unmanaged DLL files. You can get the address of each exported function using LoadLibrary and GetProcAddress, and using ordinal. But all you get is the address; it's up to you to know how to call the functions appropriately.
It would help you to understand how functions work at the binary / machine code level and how the stack works.
Here are your options:
As you said, find information from external sources like the internet.
If you can find the PDB file, this is the debugging information file, and it's how debuggers know how to associate locations in memory to lines of code. It will contain all the metadata you're looking for. But in your scenario I don't think you have access to this.
If you are comfortable with assembly and understanding executable code at the binary level, disassemble or debug the DLL. See what it's expecting, how it uses the stack.
Otherwise you will have to compromise:
Use a different library. Most practical, because honestly if you don't know anything about this sndvolsso.dll, then even if you get it "working", you will be lost once any problems arise.
Hire someone who can do this.
Just write your own code instead of using this one.
But come to think of it, your question doesn't really make sense. If you don't know what these functions are, how do you know you need them?
I got following errors when tried to calling .dll (made from delphi) function from unity3d C# code.
Here is pictures,
and errors says,
and .dll codes are,
So why error occurs and how to solve?
Thanks a lot!
Where you write
error = GetRequestResult(code);
you need to write
error = GetRequestResult(out code);
which is precisely what the second error message states.
Looking at your code, returning a PChar from the Delphi DLL the way that you do is not compatible with your P/invokes. The P/invoke marshaller is assuming that your return values were allocated with CoTaskMemAlloc and will call CoTaskMemFree on the pointer that you return. That's going to lead to some problems somewhere down the line. I think you'll need to tackle that issue at some point but since it's not the subject of this question, I won't attempt to solve the problem here.
In languages that support exception objects (Java, C#), when is it appropriate to use error codes? Is the use of error codes ever appropriate in typical enterprise applications?
Many well-known software systems employ error codes (and a corresponding error code reference). Some examples include operating systems (Windows), databases (Oracle, DB2), and middle-ware products (WebLogic, WebSphere). What benefits do error codes provide? What are the disadvantages to using error codes?
WITHIN a program one should always use exceptions instead of error codes. However, exceptions can't propagate beyond a program. Any time the error must leave the program you are left with error messages or error codes.
For simple things that will always be human-operated error messages without codes are fine. You can say "File not found" without giving it an error code. However, if it might be another computer on the other end then you should give error codes in addition. You don't want to break the other system when you change it to "File <x> not found".
I don't think I've ever used error codes in .Net except in one situation - when I was creating a console application that I knew was going to be called from another app. This other app had to know when the console app failed, and what went wrong. So, one example of when it would be appropriate would be when you know your program will be called by other programs, and you want a structured way for them to understand errors.
That said, I was a newbie to .NET at the time, and have never used error codes since.
As a side note, as a Windows guy, it's nice to be able to plop in an error code and come up with a KB article, so an error code combined with good documentation and the ability to find it = nice feelings from your users.
Very common for web service interfaces. It's very easy and standard to return a code with a description.
I agree that for most of the scenarios is old school
I'd say the biggest disadvantages it's the quality of code. You have to add more complex logic to manage error codes while exceptions are bubbled without having to use method parameters or return values.
You also have to add an "IF" to check if the returned code is SUCCESS or not, while exceptions goes directly to the error handling block.
I'm a newbie to stack overflow but...
I believe that error codes tend to be used or useful for dealing with erroneous situations that require an end-user of sorts to get involved to rectify a situation. If your code is to be maintained by another developer then exceptions is the way to go. However, in a situation where there is a problem:
in the environment that your application is running
with communication between your app and some other entity (web server, database, socket, etc)
that a device or device driver indicates (hardware failure maybe?)
then error codes may make sense. For example, if your app attempted to log into a database on behalf of your end-user, but the DB was unreachable for authentication (DB is off-line, cable is unplugged) then an error code/description combo might help the end-user rectify the problem.
Again at the developer/engineer level who will be able to touch the source code (traditional debugging and testing techniques) and modify it, use exceptions.
Hope this helps...
--jqpdev
I frequently use error codes when an error needs to be conveyed to the user, since they can be internationalized. For example, in a compiler, if there are errors in user code, errors can be signaled in the compiler backend, while the frontend can localize them into culture/language-specific strings for user consumption. Enums may be better for this purpose than raw integers, however.
I've also used them in creating an "error reporting" framework for the app. When exceptions were thrown, they were thrown with an error code, which, when the exception bubbled up, was sent (with a log) to the central server. The code helped organize the database so we could inspect logs related to a specific error.
Finally, as mentioned in a couple other answers, error codes are easy and language-agnostic to google (think Windows error codes/MS KB articles), so an error code with a description of what went wrong may be better for end-users of a technical product.
The idea of error codes is useful, but IMO they belong as exception members or as parameters to an IErrorReporter interface or something more ofthen than as method return values.
Error codes are old-school. They are of little to no value at all.
The only possible value to an error code is that it can identify a very specific circumstance. You could have a code for each point in the code base that can throw an exception. This would allow you to narrow down very precisely what the problem must be.
But nobody cares about that level of detail. Who wants to maintain such a mess. It would leave you with codes that meant something like "condition A and B but not C due to state S". It's more effort than it's worth to try to work out exactly what that means. A stack trace will be more valuable in telling you where in the program the problem occurred.
I learned to program computers before exceptions were a widespread technique. I'm so glad we got exceptions instead!
C#, and probably Java too, supports a better exception handling control flow, the finally keyword, which makes things a little nicer than using error codes. An exception object can contain any level of detail, certainly much more than an error code. So the exception object is way more practical, but you might run into an uncommon case where an error code would be more appropriate.
FWIW, C++ also supports exception objects. I don't think that C++ supports a finally keyword (though the newer C++ whatevers just might), but in C++ you also have to avoid things like returning inside a catch handler.
Error codes were designed in an age where the only way for a function to tell the caller that something went wrong was to assign a special meaning to one or more values of those which can be returned, and very frequently only a native integer or so was available for returning that special value.
For instance, in C the "get character" routine returns the next character value in ASCII, but returns a negative value if for some reason something went wrong. You are then responsible for returning to YOUR caller in a way so this error situation can be handled, and that must return etc.
The Exception mechanism is an elegant way to handle this "this is an emergency, we must return from code until something can deal with the problem". Error codes are inferior to this.
I've written many web services that are consumed by other (remote) applications. When things go badly with a request, customers more or less insist on getting a code, so that they don't have to do some horrific string comparison to find out what went wrong.
Take HTTP result codes as a fine example of this sort of behavior. "200" means happy, "300" could go either way, "400" or "500" means start freaking out.
Error codes are for if you want to send them to the user. If not, use an exception.
Sometimes you don't want to give too much information to the user when an error occurs. For example, a user is not able to sign a new contract. The error message only states something generic like "Cannot sign a new contract".
This adds difficulty to support cases where the user thinks this is not correct. If you have an error code, for example a number or an acronym, it could be part of the error message. The user wouldn't know what it means but the support staff could look it up and could then check if that specific reason for declining the new contract is indeed an error or not.
I am trying to use Adobe IFilter to search PDF files. My code is written in C# and I am using p/invoke to get an instance of IFilter:
[DllImport("query.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private extern static int LoadIFilter(
string pwcsPath,
[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
ref IFilter ppIUnk);
This works for most PDF files except a few for which this method returns -2147467259 and ppIUnk is coming as null. Does anyone have this type of errors or have any suggestions of how to figure this out?
See the MSDN docs for the LoadIFilter function - you should be getting one of the error codes, at least according to that page.
E_ACCESSDENIED = 0x80070005 -
The function was denied access to the filter file.
E_HANDLE = 0x80070006 -
The function encountered an invalid handle, probably due to a low-memory situation.
E_INVALIDARG = 0x80070057 -
The function received an invalid parameter.
E_OUTOFMEMORY = 0x8007000E -
The function did not have sufficient memory or other resources to complete the operation.
E_FAIL = 0x80000008 -
The function encountered an unknown error.
(Also, the full set of constant values is listed here, which seems to be rather longer than that listed on MSDN.) Now, the interesting things is, your error code corresponds to 80004005 in hex, which isn't listed on either or those pages. I'm suspecting pinvoke.net may have it wrong however, as many other sites (this for example) list it as E_FAIL... not that it really helps anyway. Sorry for the inconclusive answer, but maybe it will point you along the right track at least.
Edit: This error seems to have been documented elsewhere and caused much confusion to many people, with no simple solution. It seems like the cause could be one of several in fact... There's various suggestions here and here that you may want to try, but I don't think I can help you any more than that, as I have never encountered this error myself in this context. Good luck anyway...
Here's how I solved it:
Uninstall Adobe Reader/Acrobat. Install the latest version (again). It should fix the pdf filters. Good luck.
To get around the E_FAIL for Adobe 10.x, please see https://stackoverflow.com/a/8841476/455322
I got the same result when running LoadIFilter until I found this post which pointed me to the solution:
Make sure that your build platform target is x86 and avoid running the application from within Visual Studio.
Also, if you can move your code that interacts with the IFilter into a separate .exe application with the name filtdump.exe, you will simplify your code considerably.