|
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
|
|
|
|
|
VS 2013
Hi
How do I create a New (non MFC class) in VS 2013 (MFC Project).
Regards,
Bram
Bram van Kampen
|
|
|
|
|
Right click the project, and choose "Add Class", then choose a C++ class.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
problem piece of code - if(Function(parm1, parm2, parm3)==0){
error - cannot convert parameter 3 from 'char [1024]' to 'wchar_t *'
parm3 is an array -
//char parm3[Buffsize1024]; //was before
wchar_t parm3[Buffsize1024]; // I changed to make the application unicode enabled
Function is expecting wchar_t but its getting char[1024] from some where.
I also changed the function signature to accept wchar_t type in header file.
objective is to make the whole code UNICODE enabled. I have added all the tchar.h, wchar.h fiels
param 3 has been defined (modified) as wchar_t in all the connected files too.
I also tried to cast the string and pass the casted string.
CString s(parm3)
LPCTSTR p = s;
no change in the error.
I cant debug yet. Pls help how can I get rid of this error.
|
|
|
|
|
Something in your code has not been converted correctly. Go to the source code and look carefully at the line where the error occurs. Also please use <pre> tags around your code so it is more readable, like:
wchar_t parm3[Buffsize1024];
if(Function(parm1, parm2, parm3)==0){
|
|
|
|
|
Have you tried
Project properties->Configuration properties->Char set->Unicode
|
|
|
|
|
when the menu so long, for example, the favorites of IE. how to let menu auto show next item when mouse on the bottom arrow of the menu
|
|
|
|
|
|
Visual Studio 2008, C++
Class C_AR2_Messages process telemetry messages. The configuration is quite complex so class C_Configuration_Manager reads a configuration file and loads the data into the structures/variable of C_AR2_Messages.
Please note that my work is on a classified computer and I cannot cut and paste from there to here. I probably put in a few typos. Please note them, but take that into consideration.
The message class contains:
The messages class contains
Class C_AR2_Messages
{ …
WCHAR m_common_parameter_names _NAMES[ COMMON_ARRAY_SIZE ][ MAX_PARAMETER_NAME_LENGTH ];
All the below fail compile but to me, they match the examples I have found in my searches. Two of them claim that the open brace is a syntax error.
WCHAR m_test[ ] [ ] = { {L"one"}, { L"two" }, { L"three" } };
WCHAR m_test_2[ 3 ] [8 ] = { {L"one"}, { L"two" }, { L"three" } };
Wstring m_test_3[ ] = { L"one", L"two", L"three" };
Meanwhile back in class C_Configuration_Manager.h, there is a method used to hand that class the pointer giving it access. It looks like:
Class C_Configuration_Manager
{ …
VOID Set_Common_Names_Pointer( WCHAR *new_pointer[COMMON_ARRAY_SIZE ][ MAX_PARAMETER_NAME_LENGTH ] );
WCHAR mp_common_parameter_names[COMMON_ARRAY_SIZE ][ MAX_PARAMETER_NAME_LENGTH ];
and C_Configuration_Manager.cpp contains:
VOID Set_Common_Names_Pointer( WCHAR *new_pointer[COMMON_ARRAY_SIZE ][ MAX_PARAMETER_NAME_LENGTH ] )
{ mp_common_parameter_names = new_pointer; }
The compiler says:
Quote: Error C2440: ‘=’ cannot convert from WCHAR *[] [34] to ‘WCHAR *[18][34]
To my knowledge, both are declared as [18][34]
So having failed on two fronts, I open a third front in this war by editing C_AR2_Messages with
Class C_AR2_Messages
{ …
Friend class C_Configuration_Manager;
Then back in C_Configuration_Manager.cpp added this to access those member variables in C_AR2_Messages
Wstring test1 = common_parameter_names[ 0 ] [ 0 ];
Wstring test1 = C_Configuration_Manager ::common_parameter_names[ 0 ] [ 0 ];
So I have failed on all three attempts to gain access to that data in the configuration class. Will someone provide the knowledge I need to accomplish this task?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 7-Jul-14 19:57pm.
|
|
|
|
|
You are getting confused between character Variables (WCHAR) and character arrays (WCHAR[][]) . Your array of names should be declared as an array of pointers and initialised with strings like:
WCHAR* some_names[] = { { "name1" }, { "name2" }, ... };
PWSTR some_names[] = { { "name1" }, { "name2" }, ... };
The wstring type is part of the STL library and is best avoided unless you are familiar with the use of all the features of that library.
|
|
|
|
|
The array of character strings versus array of pointer was a good point. I will focus my question a bit tighter.
From C_AR2_Messages.h
WCHAR m_test_1_[COMMON_ARRAY_SIZE][MAX_PARAMETER_NAME_LENGTH];
COMMON_ARRAY_SIZE is const unsigned int = 18 and MAX_PARAMETER_NAME_LENGTH is same = 34, this is 18 WCHAR strings, each 34 characters long. I verified this by writing the following code in the constructor and stepping though with the debugger.
wcscpy_s( m_test_1[0], MAX_PARAMETER_NAME_LENGTH, L"[0] 1234"
wcscpy_s( m_test_1[1], MAX_PARAMETER_NAME_LENGTH, L"[1] 1234"
wcscpy_s( m_test_1[2], MAX_PARAMETER_NAME_LENGTH, L"[2] 1234"
wstring test_5 = m_test_1[0];
wstring test_6 = m_test_1[1];
wstring test_7 = m_test_1[2];
Actually, the strings “1234” were continued out to about 25 characters to ensure the length was greater than 18.
The wstrings were used as a very convenient tool to capture the text from the strings and see what was there. I am confident that in C_AR2_Messages I have a good declaration for m_test_1 as an array of 18 character strings, each 34 characters in length.
The Goal
In C_Configuration_Manager the code reads a text file and initializes a bunch of parameters. It needs to read and right to those strings contained by m_test_1. My plan is to hand it the address of m_test_1. I am having difficulties with that. Here is what I have come up with.
In C_Configuration_Manager.h
WCHAR *mp_common_parameter_names [COMMON_ARRAY_SIZE][MAX_PARAMETER_NAME_LENGTH];
That is to be a pointer to the array of strings. Then in C_Configuration_Manager dot CPP is the definition of the method to set the pointer:
Void C_Configuration_Manager::Set_common_names_Pointer(
WCHAR *new_pointer[ COMMON_ARRAY_SIZE][MAX_PARAMETER_NAME_LENGTH] )
{ mp_common_parameter_names = new_pointer; }
That did not work so well. The compiler says:
Quote: 1>e:\visual|studio\ar2_message_app_v11.1\ar2_message_app\c_configuration_manager.cpp(120)
: error C2440: ‘=’ : cannot convert from ‘WCHAR 8[][34] TO ‘wchar *[18][34]’
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
Note that the first [ ] in the error message is empty. None of the declarations have an empty [ ].
With the thought of arrays of strings versus arrays of pointers I tried changing the declarations in C_Configuration_Manager to:
WCHAR * (mp_common_parameter_names [COMMON_ARRAY_SIZE][MAX_PARAMETER_NAME_LENGTH] );
Note the added parens and the attempt to set order of operation and get a pointer to an array of strings. The error message was exactly the same.
So, …, how do I declare a pointer to an array of strings in the configuration_manager?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
I think rather you are widening it. Your code will be far more flexible if you use dynamic arrays, rather than fixed size, and even use vectors and strings from the STL. Apart from anything else you will avoid all these annoying compilation issues.
|
|
|
|
|
Hello Richard,
Re: Your code will be far more flexible if you use dynamic arrays, rather than fixed size ....
That will not work for this project. It is a telemetry project and the data arrives fast and furious. We are collecting data from very expensive missiles that travel very fast.
I tried using an std::map. It made writing the code much easier, but it was definitely far to slow. Too much overhead in the template libraries. When I was testing the std::map, I stepped into the code to fetch an integer. After about a hundred function calls to get that integer I knew that was the problem. The buffers from the sender were overflowing constantly. As soon as I deleted the map and went to a fixed size array, the cpu time of this application went down to about 1%. I started four copies for four different streams on one machine and had no problem.
The wstring is used when I am reading from a file as the tokenizer needs it. The data is saved in WCHAR (wchar_t) and for real time is used in that format.
So, for a re-cap. I want a declaration of N number of WCHAR strings as in:
WCHAR names[ STRING_COUNT ][ SIZE_OF_STRING ];
I have that working.
Then I want a procedure to take the address of that array of strings and hand it to a local pointer as in:
void Set_Local_Pointer( WCHAR * new_pointer[ STRING_COUNT][SIZE_OF_STRING ] )
{ mp_local_pointer = new_pointer; }
What is the right way to declare mp_local_pointer?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
The problem you have is that you cannot declare an array and then 'set it' to point to something else, it is not modifiable. The only way to do it (but I would not recommend it) is to use simple pointers, and let the code in your class figure out the length of each string in the array; in your case this is the fixed value SIZE_OF_STRING . So your code would need to be implemented like:
WCHAR* names;
void Set_Local_Pointer(WCHAR* new_pointer)
{
mp_local_pointer = new_pointer;
}
WCHAR names[ STRING_COUNT ][ SIZE_OF_STRING ];
wcscpy_s(names[0], L"StringZero");
wcscpy_s(names[1], L"StringOne");
wcscpy_s(names[2], L"StringTwo");
Set_Local_Pointer(names[0])
A better way would be to let the class object manage all the data by sending it the strings as a list, or with an index telling it which element to modify. something like:
BlahBlah class
{
WCHAR mpNames[STRING_COUNT][SIZE_OF_STRING];
void Set_Local_Pointer(WCHAR new_pointer)
{
memcpy(mp_local_pointer, new_pointer, STRING_COUNT * SIZE_OF_STRING);
}
void Set_Local_Pointer(WCHAR pLabel, int index)
{
memcpy(mp_local_pointer[index], pLabel, SIZE_OF_STRING);
}
|
|
|
|
|
The configuration class is to make changes to several data items needed by the AR2_Messages class. I don't think memcpy will provide the results needed. If I can do memcpy, then when can I not just set a pointer.
I have been nibbling around the edges but cannot quite get a full chomp on this cookie.
I have created a Console App project just to test the concept. While doing that I discovered that a Console App gets indigestion on WCHAR so I changed to wchar_t. Here is my best effort so far.
#include "stdafx.h"
const unsigned int COMMON_ARRAY_SIZE = 10;
const unsigned int MAX_NAME_SIZE = 34;
void Set_Pointer( wchar_t new_pointer[ COMMON_ARRAY_SIZE ][ MAX_NAME_SIZE ])
{
wchar_t local_pointer[ COMMON_ARRAY_SIZE ][ MAX_NAME_SIZE ];
wchar_t one[ MAX_NAME_SIZE ];
wchar_t two[ MAX_NAME_SIZE ];
local_pointer = new_pointer;
wcscpy_s( one, MAX_NAME_SIZE, local_pointer[0] );
wcscpy_s( two, MAX_NAME_SIZE, new_pointer[1] );
};
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t names[ COMMON_ARRAY_SIZE ][ MAX_NAME_SIZE ];
wcscpy_s( names[ 0 ], MAX_NAME_SIZE, L"[0] 123" );
wcscpy_s( names[ 1 ], MAX_NAME_SIZE, L"[1] 123" );
wcscpy_s( names[ 2 ], MAX_NAME_SIZE, L"[2] 123" );
wcscpy_s( names[ 3 ], MAX_NAME_SIZE, L"[3] 123" );
Set_Pointer( names );
return 0; }
The line marked as line 12 will not compile producing the following error:
Quote: Error 1 error C2440: '=' : cannot convert from 'wchar_t [][34]' to 'wchar_t [10][34]'
I note that there is NOT any variable/array declared as [][34]. Trying to convert from [][34] appears to be a misleading error message.
Not the line commented as line 15. When line 12 is commented out the code will copy to the local string using the argument pointer directly. So why does is refuse to set the local pointer, declared with exactly the same syntax? More important, how can I change to code to allow that pointer set?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
I explained in my previous message why line 12 will not compile, local_pointer is not a pointer it is an array. You cannot set an array equal to another array, you can only copy the content from one to another. Your use of the term pointer in some of your variable names is helping to confuse the issue, as they are not pointers.
So your code should be:
wchar_t local_array[ COMMON_ARRAY_SIZE ][ MAX_NAME_SIZE ];
wcscpy_s(local_array[0], MAX_NAME_SIZE, new_pointer[0]);
memcpy(local_array, new_pointer, COMMON_ARRAY_SIZE * MAX_NAME_SIZE);
If you want to use pointers then you must remember that all you are doing is keeping a pointer to the original data array in memory like:
wchar_t* local_pointer;
local_pointer = new_pointer;
So you must now ensure that that original data is not overwritten.
|
|
|
|
|
Well that was rather stupid on my part. This is what I want to accomplish in the procedure:
void Set_Pointer( wchar_t *new_pointer)
{
wchar_t *local_pointer;
wchar_t one[ MAX_NAME_SIZE ];
wchar_t two[ MAX_NAME_SIZE ];
wchar_t three[ MAX_NAME_SIZE ];
wchar_t four[ MAX_NAME_SIZE ];
local_pointer = new_pointer;
wcscpy_s( one, MAX_NAME_SIZE, local_pointer );
wcscpy_s( two, MAX_NAME_SIZE, local_pointer[1] );
wcscpy_s( three, MAX_NAME_SIZE, local_pointer[2] );
wcscpy_s( four, MAX_NAME_SIZE, local_pointer[3] );
};
The three lines that cannot convert have the error:
Quote: Error 1 error C2664: 'errno_t wcscpy_s(wchar_t *,rsize_t,const wchar_t *)' : cannot convert parameter 3 from 'wchar_t' to 'const wchar_t *'
The caller is to pass a pointer to an array defined in the manner
wchar_t some_array[ x ] [ y ];
where x and y are named constants.
What needs to be done?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 4-Jul-14 7:23am.
|
|
|
|
|
local_pointer[1] is the second character in the array pointed to by local_pointer . I'm sorry to have to say this, but you really need to spend some time studying the differences between, and the uses of, pointers and arrays in C++.
|
|
|
|
|
|
Hello Richard,
I know I am trying your patience and I am quite grateful for your help.
I went back to your example and changed my code to match. That version works partially, but it does not use the pointer in the procedure to access the data. Look first and the call to the procedure in main and the two critical lines of code:
wchar_t names[ COMMON_ARRAY_SIZE ][ MAX_NAME_SIZE ];
Set_Pointer( &names[0][0] );
That is the only way I can get a good compile. Given that, here are the critical line from the procedure:
void Set_Pointer( wchar_t *new_pointer)
{
wchar_t *mp_local_pointer;
mp_local_pointer = new_pointer;
wcscpy_s( one, MAX_NAME_SIZE, mp_local_pointer );
wcscpy_s( two, MAX_NAME_SIZE, mp_local_pointer[1] );
...}
The first copy with source string mp_local_pointer produces the desired results. But there are more strings there to be accessed. The second line with
mp_local_pointer[1] will not compile. When the array dimensions are added in, I get back to where I started.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
bkelly13 wrote: I get back to where I started. Hence the final comment in my previous answer.
The issue is that you are trying to copy data from a multi-dimensional array using a single dimension pointer; and given the structure of your original array you have no other choice. If you used the memcpy option which I suggested and just copy the entire array to a new array inside the class then you have your data correctly saved.
If you want to do it the hard way then you can do this:
wchar_t savedItems[COMMON_ARRAY_SIZE][MAX_NAME_SIZE];
wchar_t* localPointer;
localPointer = new_pointer;
for (int i = 0; i < COMMON_ARRAY_SIZE; ++i)
{
wcscpy_s(savedItems[i], MAX_NAME_SIZE, localPointer);
localPointer += MAX_NAME_SIZE; }
If you want to do it with individually named items then just copy them in order, remembering to add MAX_NAME_SIZE to localPointer after each copy.
|
|
|
|
|