C :: API Design - Handling Platform Specific Errors
Mar 6, 2015
Consider the situation where you're writing a cross-platform library that has functions for creating and manipulating windows. To create a window suitable for OpenGL rendering on Windows you need to call a bunch of different functions such as GetModuleHandle, RegisterClass, CreateWindow, SetPixelFormat, GetDC, wglCreateContextGetDC and so on. All of these functions can potentially fail in one way or another and the Windows documentation doesn't even attempt to list all the different error codes that these functions return.
Let's say your function for creating a window is called create_window(). What kind of error message should you return if any of these platform specific functions fail? It could be something as simple as MultiByteToWideChar failing for some reason when you convert the UTF-8 string your API uses internally to the UTF-16 string that the Windows API expects. I've read that you're not supposed to leak implementation details from your API but what are you suppose to return when an encoding conversion fails that is supposed to be completely transparent to the user of your library?
I've decided to simply return an error code indicating a platform specific error in those cases because there's no way I can anticipate all the different errors that the Windows API might return. The problem with that is that almost all the errors end up being platform errors and without a way to actually tell what the error is then you're basically saying that some unknown error happened. That's not very useful. You could have a function called get_platform_error() that you call to obtain the exact platform error that occurred, but I'm not a fan of such a design and I've had enough trouble with GetLastError() style error handling as it is.
An alternative to that is to create a struct that represents errors and have a field in it where you can store platform specific errors. I believe this is what Apple do with their NSError error object but I'm not sure how I feel about that approach.
Assuming you implement a way to return the platform specific error code then the next part is obtaining a string that describes the error. Your library can obviously not implement error strings for every single error code that Windows might return so you're forced to use FormatMessage. The problem with that function is that you can't force it to return error messages in a specific language and it's not even guaranteed that you can get an English translation of the error string. I don't want to display error messages that mixes English with the current language the operating system uses so how do I solve this problem?
Are there any good open source libraries I could study? The ones I've looked at so far have subpar error handling at best. Many of them don't even check the return values from platform specific functions.
I discussed a topic about how to write cross-platform file IO code with a member named Disch for about a year ago. Since I am a beginner I am not sure if the "rules" for doing this has changed or not within C++.
He taught me that differenct CPU:s use different endianness when reading and writing to files. However, why can't the C++ standard file IO functions detect what endianness should be used on the current machine that is running the program? Why didn't the developers who created the standard library develop file IO functions that are cross-platform from the beginning? Have the rules changed since last year?
What I learn is that if you need to store data in files that will be read and written to on different machines, you have to define in the program what endianness should be used. For example, if I needed to store 4 bytes, I had to do this manually with my own functions and define in those which endianness is used.
System();is bad, I get that. Is there another way, that works across platforms I can use to execute an external program. If not, is there a windows specific way.
I am planning to develop a GUI that will run on Windows, Linux, Android & iOS. If am right, VC++ apps don't work on Linux, Android & iOS. Is that correct?
Is there an alternative to that? Which other tool works for GUI development on above mentioned 4 platforms?
In my game, I want to display dialogues for exceptions, and I don't really want to use something heavy like Qt, but then I also don't want to write and maintain my own platform-specific code to do it. Is there any library that can display simple message dialogues, just a few lines of text and an OK button, without being complete overkill like a full-blown GUI library?
I am starting a turn based battle (similar to pokemon) app. How could i make this and make it cross platform. Also is it possible to make it access gps and allow other devices with the same app communicate with each other?
I have done things on the command line but i never made anything with images so i dont even know where to start for this app.
The project builds on Win32 platform, but not on x64.
Full error message: dllentry.obj : error LNK2001: unresolved external symbol "class CFactoryTemplate * g_Templates" (?g_Templates@@3PAVCFactoryTemplate@@A)
The dllentry.cpp (a DirectShow base class) compiles on both platforms. It contains the external declarations:
extern CFactoryTemplate g_Templates[]; extern int g_cTemplates; g_Templates[] is then used in two functions: __control_entrypoint(DllExport) STDAPI DllGetClassObject(__in REFCLSID rClsID, __in REFIID riid, __deref_out void **pv)
[Code]...
myClass.cpp contains the definitions for the two externals in dllentry.cpp, at top level, just after the includes:
CFactoryTemplate* g_Templates=0; int g_cTemplates=0;
myClass.cpp also compiles by itself, but the project does not build. I checked all the libraries in the project settings and all seems to be OK, the 64 bit versions are used.
What should I do to make the project build for x64 platform?
I'm working on a project which uses gtk+ and gtkmm. We use them in preference to MFC because the program needs to be cross-platform. For quite a long time, customers on OS-X and Linux have sometimes complained that the program would crash during shutdown but the Windows version (which I work on) never seemed to suffer. However, I'm now transferring my build environment to a new PC and I'm noticing the same shutdown crashes. It's a bit complicated so let me start with a small example:-
Code: namespace Whatever { class B { public: virtual ~B(); private: int bb;
[Code] ....
Suppose I run the above program. When it stops at breakpoint #1 I make a note of the value of pA. Eventually the program reaches breakpoints #2 and #3. At each point my this pointer is exactly the same number. If the value of pA was 0x03604fb0, my this pointer is identical at both stages.
Suppose I run the real example. At breakpoint #1 the value of pW is 0x03604fb0. But by the time I reach breakpoint #2 my this point is slightly different:- 0x03604fcc. It doesn't seem right to me and I'm wondering if it might be contributing to our shutdown crashes.
I am overriding OnSaveDocument in my MFC document class to strip out the carriage returns when saving my app's document to a UNIX file system but not when the user is saving a file to a Windows file system.
Is there a way to determine if the lpszPathName in OnSaveDocument(LPCTSTR lpszPathName) is a UNIX or Windows file system?
Note, I want to avoid hard coding server names and I want to avoid overriding the FileSave dialog and forcing the user to select Windows or UNIX.
My need is that i need to design a program for a machine. The machine takes "x"qty of load, refines 30% of it and sends back the 70% to the initial position. after how many times, does the qty of the load refined will be equal to the initial load and how many times does it need tot be refined?
I have two Form Employee and Instructor, here Employee is super class of instructor.
public class Instructor: Employee { //Instructor class }
I am able to access all property of Employee inside instructor class but with these some textbox and button design inside Employee design is also getting inherited inside Instructor design and i don want this and i want to maintain the parent-child relationship between Employee and Instructor.
I want to write a command line parsing library that is very flexible in terms of parsing style but I'm not able to design a mechanism that satisfies this requirements.
Generally i want to have a class that contains all the necessary information about how the command line has to be parsed.
Code: // draft class style { public: enum class type { // the basic style type
[Code] ....
Need completing the draft shown above, because for every basic style type there is an own set of extensions that applies only to this one specific style type.
Code: // how a style object should be created style parsing_style(style::type::posix, style::extension::gnu|style::extension::subcommand);
How to design the class. (using c++11 features like std::enable_if is fine)
1. Design 1: Have an independent class to handle everything and "friend" it with the users. Pro: Only 1 interface needed and we can switch interface easily. Con: There is a lot of inter-class data which destroys encapsulation
class CalibratorXML; class Device{ string sourceLabel; string destLabel; vector<CalPoint> calPoints; friend class CalibratorXML;
I have a Class called 'DataManager' which contains a list of my 'DataItem' objects (this are created by an XML file).
I have also created some custom controls which, among other things, has a property to link it to a "DataItem" object.
My question is, is it possible to create an instance of my DataManager class at design time (which runs all the code as it would at run time to create all the DataItems from the XML)?
I want to do this so that I can update my DataItem property in my custom controls to use a UITypeEditor which then allows me to link to a DataItem at design time.