|
Which line gives the error?
And please use <pre> tags around your code.
|
|
|
|
|
You have to format the filename string.
Also need to allocate memory to it.
char filename[1024];
sprintf(filename, "%s\\EUR_PR_01302013.txt", current_dir);
|
|
|
|
|
Environment Windows XP and 7, Visual Studio 2008, C++.
Experience level: novice with TCP/IP, but do have the application working with CAsyncSocket.
The message application receives telemetry data from some hardware (arrives fast and furious), processes it and dispatches the results to a display device via TCP/IP. It sends the data to the client but does not receive from the client.
Setup: the TCP “Manager” is started by the main application and it listens for the client. On connect, it creates the “Sender” that does all the sending.
Question: what is the best, read that simple and maintainable, way for the manager to get the Sender pointer to the main application so it can use the Sender? When the client closes the connection, how is the application informed of that?
Tentative proposal: the application provides the Manager with a pointer to itself. When the connect is made the Manager calls an application method such as
Set_TCP_Sender_Pointer( C_Sender_Class *new_sender_pointer );
The Manager can pass the pointer (to the main application) to the Sender on creation. When the Sender gets a close from the client, it calls the same method using a NULL for the argument. This makes the application aware there is no Sender. Then the Sender exits. (There is only one thread so I think there is no concern about the application using a pointer to a non-existent Sender.)
I am pretty sure that will work, but if you had to pick up on this project, would it be easy to understand and maintain? Presume you are not an expert with TCP/IP in the Microsoft world.
I am open to alternative suggestions.
Thanks for your time
|
|
|
|
|
After writing the OP I outlined the process with all the steps then began to implement.
I have started using a common directory for re-usable code. In order for the Manager to call a method in its creator it must know the name of the owner's class. To do that I added a forward declaration in the Manager.
However, this means that when the class is re-used, and the owner has a different name, the Manager must be changed.
There is another indicator this is a bad practice. After the forward declaration in the dot H file, the dot CPP file needs to reference the dot H file of the owner. The common code resides in another directory and it cannot find the central code of the main project unless it is specifically spelled out. Then, if the project is moved or re-name, the common code must change.
There may be a way to do this automatically using directive and path names within Visual Studio. But I now think that even if that can be done it would be miss-guided.
Conclusion:
A utility class can call "down" the heiarchy to objects it creates, but should not call up to its owner. While the concept of it calling up may make the owner code neater, it represents an inversion of authority. (Regardless of how the methods are name, a call up in an inversion.)
Problems remain:
The main application uses pointers into the lower level objects to accomplish its tasks. How does it become aware that the lower level needs to exit, or maybe even has already exited.
Controlled exits:
When the TCP sending function detects that the client has closed the connection it does not exit right away. It waits until the upper level code calls the method to send the data. Then the lower level returns an error code stating that the data has not been sent and that the object is exiting.
Uncontrolled exits:
I generally do not like exceptions, but this appears to be a time when an exception is warranted. When the lower level must terminate unexpectedly, then an exception could work its way back to the upper level and the exception handler can NULL the pointer.
Thanks for your time
|
|
|
|
|
I personally never used exceptions for ATL projects. Now a colleague votes strongly for throwing exceptions instead of returning HRESULT s. I did some research, and even here on codeproject I don't find much about ATL and exceptions. To me it appears that exceptions in ATL are not very popular. So I would like to know from you ATL guys:
Do you use exceptions? Or you simply return an HRESULT ?
And most imporant: Why?
Cheers
Imagiro
|
|
|
|
|
I used a combination. A colleague and I developed an exception object with various constructors that could, for example, be thrown with parameters of: a HRESULT, a string describing what failed, the file name it was thrown in, and the line number of the file it was thrown in.
for example:
HRESULT hr = somefunc();
if(FAILED(hr))
{
throw _abc_error(hr, L"Somefunc failed", FILE_AND_LINENO);
}
...
catch(_abc_error& err)
{
MessageBox(err.ErrorMessage(), L"App Name", MB_ICONWARNING | MB_OK);
}
The exception object constructor then took care of writing the error as an event to the System Event log as well as looking up any messages from the HRESULT, or accessing any other error handling mechanism needed.
This resulted in code unclutered with complicated error handling and a standard way of making sure that the log showed not only an error code or message, but a comment on what was being attemted, which .cpp file this was detected in and the line number of the file.
Jonathan
|
|
|
|
|
Personally: I never did like exceptions and avoid them. I did a google on "exceptions C++ pros and cons" and found several discussions. There are some strong opinions on this.
I would much rather test for bad conditions and return an error code as opposed to throwing an exception.
Thanks for your time
PS: This is a nice discussion:
C++ Exceptions: Pros and Cons[^]
|
|
|
|
|
I agree when you say you'd rather return an error code than throw an exception. I found using exceptions locally makes the code clearer and imposes some structure. Having caught the exception locally and dealt with it i.e. usually recorded what exactly went wrong etc I too then prefered to return an error code.
Jonathan
|
|
|
|
|
Having originally started with C, I too prefer returning an error code to be cleaner than catching exceptions, but I'm sure others see exceptions as the true C++ method for handling errors.
|
|
|
|
|
Windows XP and 7, Visual Studio 2008 and 2010, MFC, C++
Application A supports Unicode and application B does not. Class Log_Writer does not support Unicode. Both applications use Log_Writer ok passing a char * to log writer.
The problem is within Log_Writer when it cannot open the log file. It uses AfxMessageBox() to display an error to the user. When Log_Writer is compiled within the A environment the error message is:
None of the 2 overloads could convert all the argument types.
What can be done so that Log_Writer will work in both environments?
Thanks for your time
|
|
|
|
|
What is that actual code where the error is given?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
the code is
AfxMessageBox("Unable to open log file");
If I pass it a CString it is okay as in:
CString x;
x.Format( L"stuff" );
AfxMessageBox( x );
But that does not compile in the non unicode project. I am hoping for something that will compile in both worlds.
EDIT: It finally occurred to me that there might a a define for unicode,..., and there is. A bit of googling showed me that using a #ifdef _UNICODE etc will resolve the problem.
Still, is there a way to write the code that will compile in both worlds?
Thanks for your time
modified 14-Jan-13 15:03pm.
|
|
|
|
|
x.Format( L"stuff" );
is wrong for ASCII based compiles, since the L prefix generates a Unicode string. Use the _T() macro around all your string and character constants and they will be correctly generated as ASCII or Unicode depending on the project settings. Note, do not forget to #include <tchar.h> for the foregoing macro.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
bkelly13 wrote: EDIT: It finally occurred to me that there might a a define for unicode,..., and there is. A bit of googling showed me that using a #ifdef _UNICODE etc will resolve the problem. You should use the Project Properties -> General -> Character Set, to select Unicode or ASCCII for your project.
|
|
|
|
|
If you are writing code for windows NT and not for Win9x and its friends then forget about the A version of your program and simply stop maintaining it. This results in much less trouble for programmers, cleaner easier to read code. Actually WinNT uses utf16 internally so all A functions are just functions that do string conversions before and/or after calling a W version. Win9x is almost dead and the same is true for the A version of programs that is simply a heritage from old times.
|
|
|
|
|
You have a good point. My problem (and notice my phrasing) is that I dislike unicode. All the various option make dealing with it an absolute pain.
If you (you in the general sense) want to use computers, then it is time to exit the hieroglyphics epoch and move into the age of alphabets. If you cannot fit your character set into 256 limit then, in my not so humble opinion, you do not have an alphabet.
Yeah, I know I may be in the minority, and no, this is not intended as the start of a flame war. Just my opinion.
I have spent "WAY" too much time trying to figure out the syntax of dealing with unicode. And finally, I work in the military world and my code will never ever be used in the world of hieroglyphs. Straight up ASCII is much easier to deal with.
Thanks for your time
|
|
|
|
|
You have to deal with unicode if you make applications to sell worldwide. 256 characters isnt enough for example for chinese character sets (maybe for simplified chinese) and china is a big market. Unicode isnt so painful if you use utf8 that is more natural to use in C/C++ program because you can write "string" instead of L"string" and you have to convert from utf8 to utf16 only when you call winNT api. Thats what I usually do and this way your program becomes easy to port to other platforms too (linux, mac, ...).
|
|
|
|
|
Review all the macros available to help with unicode/non-unicode compilations from MFC:
What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^]
If you don't want to use MFC, you can always make your own macros to do the same thing... they're all relatively straight forward.
|
|
|
|
|
I try to create MDB file with the MDW file from x64 bit application in x64 machine.
the API "SQLConfigDataSource" fails while compiling my code for 32 bit architechture works fine.
_bstr_t bstrAttribute = "CREATE_SYSDB=\"" + bstrPathMDW + "\"";
BOOL bResult = SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, L"Microsoft Access Driver (*.mdb)", bstrAttribute))
{...
}
So the problem appears when i compile my code for x64 bit architechture.
Please any help its so urgent.
|
|
|
|
|
khaliloenit wrote: So the problem appears when i compile my code for x64 bit architechture. Please explain what problem; we cannot see your screen, nor guess what you may be doing.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The problem is not related to compilation error.
The compilation is Ok.But when i run my code the API above fails.
The question is what is the reason for its fails from x64 architecture application on x64 machine.
NB: I work with mdb files
|
|
|
|
|
khaliloenit wrote: when i run my code the API above fails. Well, as I said, we cannot see your screen so we have no idea what may be happening.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hello All,
I have a CAB file which has a COM DLL and .INF file. My application is embedding this component in a JSP page and it is working fine in IE7 & IE8 but failed to work on IE9.
Any suggestion on how to solve this issue
|
|
|
|
|
Where can I get books on ActiveX and COM. I started with an ActiveX book (ATL) and it began by stating I must know COM. I wound up with Dale Rogerson's "Inside Com" Its on Amazon but no soft copy. I don't want any more hard copy books.
Every where I find a PDF or ebook version they want me to first down load their downloader. Their code could have anything in it so: No! to that.
So: What books do you recommend to a novice that needs to write an ActiveX component. The container is a commercial product and I need to write a video display for some very custom video input.
I am happy to pay, but it must be a soft copy.
Thanks for your time
EDIT: Another book I need: Win APIs.
The APIs and method that are needed to directly use TPC/IP. I want the ability to bypass all the cycle stealing intermediate classes and go directly to the API. I am having a difficult time with my searches on this and prefer a book that someone recommends rather than a stab in the dark.
-- modified 2-Jan-13 10:38am.
|
|
|
|
|
You mentioned Inside COM by Dale Rogerson which remember using so I had a glance at my bookshelves. I know Inside COM taught me the underlying principles of interfaces and COM without resorting to ATL or MFC.
Professional COM Applications with ATL (Wrox, 1999) was quite good on Active X, and does have a section 'Building an ActiveX Calendar Control' which explains a lot. However something like this, whilst being the same era as Inside COM, refers to VC6 and ATL3, not the most up to date now.
I did also buy ActiveX Controls Inside Out (Microsoft Press, 1997) and see it on my shelf but this is un-thumbed so I obviously didn't use it a lot, but its more wider ranging than the ATL book mentioning MFC and Visual Basic controls (VBXs).
I don't want to sell mine but hope this might help.
|
|
|
|