|
That may be true, but sending those values to printf is not the same as casting to pointers. You can confirm it by printing out the values of the two pointers created in my two lines of code.
|
|
|
|
|
|
Windows 7, Visual Studio 2008, C++
I am writing a TCP/IP utility that can be run/used by a non-windowing application. An MFC dialog stands in for the main app during development. The utility runs as a separate thread and communicates with the main app via events and a shared memory structure. Keeping this short:
Is there a mechanism that the TCP utility can use to trigger a method in the MFC?
Details
Presume the dialog has: OnBnClickedShowStatus(…) When the button is clicked this method reads some variables from the shared structure and displays them in the dialog. Can that button, or its essential code, be triggered by an event from the TCP utility?
The alternative is to create a timer and have it run every 300 milliseconds or so. I am hoping for something that is as simple as the timer and causes minimal additional coding in the TCP utility.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
A better solution would be to implement the TCP utility as a library that can be called from any other application: windows, non-windows, MFC or not. It could incorporate a call back feature that would allow it to call into the controlling app whenever necessary.
|
|
|
|
|
I don't have any idea of how to do that, but it sounds like a good concept. I did some searching and will look into that.
Still, finding this particular concept of having the thread call into the controlling app may be a bit difficult to discover. Can you give me a few words or a link that discusses that in particular?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 7-Sep-14 11:15am.
|
|
|
|
|
Export an API from the library that takes a function pointer as an argument. Then use this API from the main program to pass a function pointer into the library so it can call back to the main program.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I am having a bit of trouble with that concept. I am currently writing and testing this utility, a TCP/IP class that is run under a thread separate from the main app. The main app instantiates a class call C_Log_Writer to write data to a log file. Main handed a pointer to C_Log_Writer to the thread and it tried to write some data to the log. That crashed the app repeatedly. The prompted the conclusion that since they are in separate threads each with its own execution pointer, that was not a good idea.
Creating a new instance of C_Log_Writer for the thread resolved the problem. There are two log files, but that's okay. That makes me concerned that calling a function across a thread boundary might be a problem.
That said:
When you write: Export an API from the library, I take that to mean export a function from the TCP utility. Do I follow you correctly?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
1. Yes, export a function from the utility
2. About the crashing when passing a pointer to your log class: Where did it crash? Can you pinpoint that? It might have something to do with the TCP utility using a different heap from the main exe.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
please help me coding (in language C)quine mcCluskey's method for solving upto 8,9 or 10 variables whose minterms are to be generated automatically, only the no. of minterms, no.of variables and vectors of minterms are user given
|
|
|
|
|
And please do not repost the same question.
|
|
|
|
|
can someone plez help me coding the program in C for implementing Quine McCluskey's method for 8,9 or 10 variables where the inputs are to be generated automatically. Only the no. of variables, no. of minterms and a vector of minterms are user given.
|
|
|
|
|
|
Windows 7, Visual Studio 2010, C++
structure m_common_data contains:
struct st_common_data
{...
LPWSABUF p_lpwsabuf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ];
where the constant has the value 16.
The main app dot H contains:
char m_send_buf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ][TEST_BUFFER_SIZE];
where TEST_BUFFER_SIZE is an arbitrary 1024 for initial testing.
the main app dot CPP initializes all with this, and after some debugging variables were installed looks like this:
void CTest_ServerDlg::Initialize_Send_Buffers()
{
unsigned int db1, db2a, db2b, db2c, db2d, db3, db4, db5, db6, db7;
for( int i = 0; i < TCP_SERVER_PACKET_BUFFER_SIZE; i ++ )
{
for( int j = 0; j < TEST_BUFFER_SIZE; j ++ )
{
m_send_buf_array[ i ][ j ] = ( ( i * 5 ) + j ) % 255;
}
db1 = (unsigned int) &m_send_buf_array[ i ][0];
db2a = m_send_buf_array[ i ][0];
db2b = m_send_buf_array[ i ][1];
db2c = m_send_buf_array[ i ][2];
db2d = m_send_buf_array[ i ][3];
db3 = sizeof( m_send_buf_array[ i ] );
db4 = (unsigned int) &m_common_data;
db5 = (unsigned int) &( m_common_data.p_lpwsabuf_array[ i ] );
db6 = (unsigned int) &( m_common_data.p_lpwsabuf_array[ i ]->buf );
m_common_data.p_lpwsabuf_array[ i ]->buf = &m_send_buf_array[ i ][0];
m_common_data.p_lpwsabuf_array[ i ]->len = sizeof( m_send_buf_array[ i ] );
}
When stopped on the first time through the i loop, i = 0.
The debugger shows
db1 = a valid looking address
dba,b,c, and d contain: 0, 1, 2, 3, as expected
db3 = 0X400, expected
db4 looks like a valid address
db5 = db4 + 0xF0 again expected because of items not shown.
db6 shows the value 4.
db6 is the value I do not comprehend. I expected a valid address here, specifically I expected the value to be 4 larger than db5 because the ->buf item of structure WSABUF is the second item in the structure.
Please explain why db6 has the value 4 rather than the address of item ->buf.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 14-Aug-14 11:56am.
|
|
|
|
|
Your declaration
LPWSABUF p_lpwsabuf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ];
says that p_lpwsabuf_array is an array of LPWSABUF items, that is pointers to WSABUF structures. Are you sure that this is what youe meant, rather than an array of actual structures?
|
|
|
|
|
The Microsoft web pages here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms741542(v=vs.85).aspx[^] has:
typedef struct __WSABUF {
u_long len;
char FAR *buf;
} WSABUF
Edit this line:
m_common_data.p_lpwsabuf_array[ i ]->buf ( <- delete ">buf" and retype the > ) = &m_send_buf_array[ i ][0];
If is delete the end of the assignment and retype in the ->, intellisense says the options are len and buf. Going on that I think the declaration and statement are correct. Are they incorrect?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
I'm not sure what you are saying here, but my previous question still stands. Is p_lpwsabuf_array supposed to be an array of WSABUF structures, or an array of pointers? There is a big difference.
|
|
|
|
|
I am expecting an array of structures. The MS page shows this:
typedef struct __WSABUF { u_long len; char FAR *buf; } WSABUF, *LPWSABUF;
I am uncertain as to how the phrase ..;., *LPWSABUF; is read.
The line of code in question is an assignment to:
m_common_data.p_lpwsabuf_array[ i ]->buf
When I get here:
m_common_data.p_lpwsabuf_array[ i ]
and type in the two characters ->
Intellisense prompts me for the fields len and buf. I interpert that as saying Intellisens interperts the declaration as an array of structure, each of which has a pointer to the data to be sent.
But when I try to show the address of the buf pointer, (not the value of the pointer, but the address of where the pointer is held in the structure), the debugger provides an address of 4. Since that is a contradition, there is something I am misinterperting.
Edit: I should change the name from p_lpwsabuf_array to lpwsabuf_array as the intent is an array of structure. The p_ prefix is misleading.
EDIT: She key here seems to be that I don't know how to read the declaration of WSABUF, *LPWSABUF. I tried using an array of WSABUF but WSASend demands LPWSABUF. When I use that in the declaration I am unable to assign an address to the ->buf pointer. If the original declaration is WSABUF, how is that cast to LPWSABUF so WSASend will accept it? If the other way around, how is the code to be changed to assign an address to ->buf in the code I posted?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 12-Aug-14 19:33pm.
|
|
|
|
|
typedef struct __WSABUF { u_long len; char FAR *buf; } WSABUF, *LPWSABUF;
You read the above as struct __WSABUF declares a structure whose contents are declared within the braces following. The typedef specifier creates synonyms for the following declaration, which in their turn can be modified types. In this case WSABUF is a synonym for the preceding structure declaration. *LPWSABUF is a synonym for a pointer to the structure: the preceding asterisk tells us that it is a pointer.
So the declaration
LPWSABUF p_lpwsabuf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ];
declares an array of pointers. However according to the documentation for WSASend [^], the second parameter is a pointer to an array of WSABUF structures, not pointers, so it should be declared, and initialised something like:
WSABUF wsabuf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ];
for (int i = 0; i < TCP_SERVER_PACKET_BUFFER_SIZE; ++i)
{
wsabuf_array[i].len = 16; wsabuf_array[i].buf = new char[16]; }
Then when you need to use a pointer to the array on a call to WSASEND you just use the array name, which the compiler converts to a pointer to the first element thus:
int result = WSASend(socket, wsabuf_array, count, ... etc)
Does that make sense?
|
|
|
|
|
Good Morning To You,
It is 5:50 AM here and I wanted to check for replies but am not yet awake enough to write code. It sort of makes sense. It makes enough sense for me to create a new solution and try some examples with your comments to see how they work out. I'll do that before getting back to my main project.
I really appreciate you taking the time to explain this.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Supplementary point. Since p_lpwsabuf_array is declared as an array of pointers, each element needs to be initialised with the address of a real WSABUF .
|
|
|
|
|
Hello Richard,
Your last post is proving very helpful. I have made many steps forward. Now I arrive near the final stage of getting my syntax correct.
Class C_TCP_API_Server handles all the details of sending data to a client. The data originates with these two declarations:
WSABUF wsabuf_array[ TCP_SERVER_PACKET_BUFFER_SIZE ];
LPWSABUF p_lpwsabuf;
There will be 16 buffers and p_lpwsabuf is initialized with:
m_common_data.p_lpwsabuf = m_common_data.wsabuf_array
Deep down in the send class is:
m_send_status = WSASend(
m_client_socket,
mp_lpwsabuf[ *mp_send_array_index ],
1, // one buffer for now
NULL, // I will add this later
NULL, // no flags
m_overlapped[ *mp_send_array_index ],
NULL ); // no completion routine
where mp_lpwsabuff is declared as
LPWSABUF mp_lpwsabuf;
And its value is traced back to the first declaration shown above. The problem is this compiler error:
Quote: 1>d:\bryan\common_code\c_tcp_api_server.cpp(1124): error C2664: 'WSASend' : cannot convert parameter 2 from '_WSABUF' to 'LPWSABUF'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Parameter two is declared as LPWSABUF. WSASend has LPWSABUF as its second argument. I do not understand why the compiler wants to convert from _WSABUF to LPWSABUF.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
The pointer mp_lpwsabuf points to an array of buffers ([ 0 .. N ]). However, the expression mp_lpwsabuf[N] is a reference to the Nth element in the array, it is no longer a pointer (yes, I know it sounds weird). To get a pointer to the Nth element of the array you need to get the address of (or addressof) the Nth element. You can do that with the addressof operator (& ) or by adding the index value to the pointer thus:
m_send_status = WSASend(
m_client_socket,
&mp_lpwsabuf[ *mp_send_array_index ], 1, NULL, NULL, m_overlapped[ *mp_send_array_index ],
NULL );
m_send_status = WSASend(
m_client_socket,
mp_lpwsabuf + *mp_send_array_index, 1, NULL, NULL, m_overlapped[ *mp_send_array_index ],
NULL );
Remembering that adding N to a pointer to an array of structures will actually add (N times the size in bytes of the structure).
I'm interested to know why you need the indirection operator on the mp_send_array_index variable.
|
|
|
|
|
Good Morning,
At least for me.
That makes sense after thinking a bit. Lets see if I got that right: WSASend needs to have direct access to the LPWSABUF pointer so it can increment the pointer through the multiple WSABUFs and send several buffers on a single call. Hence, give it the address of that guy.
Regarding the strange line: m_overlapped[ *mp_send_array_index ],
The main app creates and owns the buffers to output to a client via TCP/IP. It creates a structure to hold the pointers to the buffers. The pointer to the buffers are in an array of WSABUF and there are two integers, load_array_index and send_array_index. Main app uses the load_index to keep track of which WSABUF item to use next and C_TCP_API_Server (presented momentarily) uses send_index to keep track of which one to send next.
The class to manage the TCP/IP operations is C_Server_Thread and is started as a thread from main_app and the structure is passed via the one pointer.
That C_Server_Thread has events to trigger/control it and other performance monitoring variables. It creates class C_TCP_API_Server to manage the details of the TCP/IP. That class needs only a subset of the structure passed. So C_Server_Thread hands off a pointer to the index values.
Hence, the pointer to the index and the * dereference character.
Elsewhere: C_TCP_API_Server created its own thread, a completion thread. It collects the events from the I/O completion, a unique event assigned to each item in the array of buffers. It gets the event and simply NULLs the address and zeroes the length value in the WSABUF, and everyone knows that buffer is ready to be used again.
It is a bit complicated but the end application will handle a stream of 12 megabits/second and must extract parameters real time and pass them on. Some of those parameters are bit wise meaning a 16 bit word will have 16 parameters, each of which must be separated and given its own name. (Actually a tag number, which is a direct reference to the name.) Then the parameters are passed on to a display system. There will be up to four copies of this app running at one time because as many as four of these telemetry transmitters may be active at one time. It must be fast and efficient to display the data in real time. (Ok, to be very technical, very near real time) Safety must be able to see many of these parameters and have the ability to destroy it if things go badly. I have an early version of the app running but it uses blocking TCP which is can be a real pain. This app interfaces with another system and if it gets killed, those interface slots are not released causing major problems. Not counting the bit wise parameters, there can be close to 10,000 individual parameters that must each be recognized and extracted. Quite a mess but the unit under test is rather complicated and expensive. The engineers want all the data they can get.
Right now main_app is purely a test vehicle to check out these TCP server classes. So far, I have written code to test all the events to C_Server_Thread, and have successful tested the completion events. I am able to trigger and detect all the events to C_Server_Thread and all the completion events. I have code to show the addresses of all the buffer and that looks good. The test data is initialized with unique values and I am tracking it at each step of the way.
This is my first foray into the world of using the APIs to implement TCP/IP control, and of using threads. It has been the most difficult code I have written, but I am happy with my progress, ..., so far.
There is no way I could have progressed this far without help from these forums and specifically from you.
Thank for so much for your time and mostly for your patience.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 14-Aug-14 9:37am.
|
|
|
|
|
bkelly13 wrote: It is a bit complicated but the end application will handle a stream of 12 megabits/second and must extract parameters real time and pass them on. Yes, I think I recall you explaining that in an earlier thread.
bkelly13 wrote: Thank for so much for your time and mostly for your patience. You are welcome. It's easy to be patient with people who so obviously try so hard to do their own work, and just need a little assistance from time to time. And to be honest, I still struggle with a number of C++ and Windows concepts.
|
|
|
|
|
What makes this double, or triply difficult is that I am the only one in our detachment writing C/C++ code, the only one that uses Visual Studio, and the only one doing this level of work. You, and I, can imagine how much better my code would be if I had someone to present it to and review. I try to make up for that deficiency as best I can, but there will never be a substitute for another person's perspective.
So your help is even more appreciated than is immediately apparent.
Edit: And I am a bit proud of my thinking that led to creating a second thread to handle the completion events. That really simplified the class C_TCP_API_Server by removing all that stuff from the sending code and the code that manages the connection. I am sure its been done many times before, but this has been a pretty cool learning experience. PLUS, my managers do understand the complexity and I have the luxury of time.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|