|
Didn't work at first, then I hardcoded the message length just for testing, and everything worked, including passing the string to the function. I need to figure out the length calculation. Working on that now, almost there, wow
int msgLength = 80;
char* szWords = new char[msgLength + 1];
strncpy(szWords, recvbuf + 3, msgLength);
This is the szWords,
szWords 0x003e8f10 "ServerName;DELLC521-01;InstanceName;SQLEXPRESS;IsClustered;No;Version;10.50.1600Íýýýý««««««««þîþ"
|
|
|
|
|
jkirkerx wrote: I need to figure out the length calculation.
I gave you the code for that in my previous message; and I missed a line of code, so it should read:
int msgLength = buffer[2] << 8 + buffer[1];
char* szWords = new char[msgLength + 1];
strncpy(szWords, buffer + 3, msgLength);
szWords[msgLength] = '\0';
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
So I still need to terminate the string with \0
Having trouble with the length, I did quick cheat with
int msgLength = bytesReceived - 3;
char* szWords = new char[msgLength + 1];
strncpy(szWords, recvbuf + 3, msgLength);
int msgLength with is an integer, I keep getting 0, is it because recvbuf[2] is returning the string, but wait, it's a byte representing a number. hmm
|
|
|
|
|
Sorry, I really need to test my code before posting here, and check my operator precedence rules. In my defence, it is getting late and I'm half way through a nice bottle of Merlot
Try:
int msgLength = (recvbuf[2] << 8) + recvbuf[1];
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Hey no problem, I thought perhaps I had to solve it as a test of some sort. I answer asp.net server control questions on my last beer before I turn off the computer at night.
I want to thank you for helping me. It was really bring me down, and everyone just sort of gave me a blank look, "Its so easy, come on, that c++ 101". Words cannot express the gratitude for the help. I really appreciate it, and yes, code project pulls through with expert help.
|
|
|
|
|
great fun but we didnt solve the passing of byte arrays
|
|
|
|
|
oh no, it got solved! Richard wrote some code to extract the message size from byte 2, copy just the message to a new char array, termintate it with \0, and pass it to my function for processing
I'm stoked, surfer happiness, yeah!
I've been banging my head for days on this, feeling pretty stupid about it.
Thanks for watching the show!
msgLength = (recvbuf[2] << 8) + recvbuf[1];
szWords = new char[msgLength + 1];
strncpy(szWords, recvbuf + 3, msgLength);
szWords[msgLength] = '\0';
_process_SQL_BufferData(szWords, bytesReceived);
|
|
|
|
|
Happy to help, it's what makes CodeProject fun! That and your appreciation.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
is done like this:
#include <iostream>
using namespace std;
void charray(char test[]) {
strncpy(test,"test",sizeof(test));
}
int main() {
char test[256]={' '};
charray(test);
printf("Text in %s!\n", test);
return 0;
}
|
|
|
|
|
I experimented with that Friday night, and only the first byte copied over in strncpy. I think the 2nd byte bombed in the copy. The same thing happens when I pass the byte array to a function.
It must be the 2nd byte, so perhaps I need to cut off the header of the byte array, and then do the copy and pass.
|
|
|
|
|
You cannot use sizeof() in the function above, because the array size is not known at that point.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
jkirkerx wrote: When I pass it to a function, the data is lost.
What does that mean exactly?
One possibility is that you are misusing the buffer. If so that has nothing to do with sockets nor with how you chose to process the data.
|
|
|
|
|
Only the first char in the byte array passed to the function. I suspect that the 2nd byte was the data size of the packet, and the 3 byte was used for some control purpose, or an extension of the 2nd byte or something.
So when I go the function I passed the data to, just the first byte is there 'T' or something. I think it was a 0x05.
I have it working now, so far so good, and it's not crashing yet. I ended up passing the data in a vector to the function.
|
|
|
|
|
Doesn't matter what the other data was.
What matters is that it was missing.
Some possible reasons
- You copied from one buffer to another incorrectly.
- You overwrote the original buffer
- You didn't have all the data in the buffer in the first place.
- You are indexing incorrectly in the processing stream.
|
|
|
|
|
I think it's just i no clear concept of c++, and I should buy one of those thick books on it.
|
|
|
|
|
Dear Friends
I have a mfc application which I already converted to a dll. now I want to launch that application from another win32 client application. So how I can achieve this. Any h elp would be highly appreciated.
check my code snippet.
//in App.h header
extern "C" __declspec(dllexport) void runAppli();
//in App.cpp
__declspec(dllexport) void runAppli(HWND hWnd)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
}
//In the client console
int main(int argc, char **argv)
{
typedef void (*EntryPointfuncPtr)(int argc, const char * argv );
HINSTANCE LoadMe;
LoadMe = LoadLibrary(L"C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll");
if (LoadMe != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");
EntryPointfuncPtr LibMainEntryPoint;
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"runAppli");
return 0;
}
I am not able to launch with this code. Please tell me whats going wrong... My application is appearing and disappearing from the screen.
Thanks a lot for any help. Sujan
|
|
|
|
|
sujandasmahapatra wrote: My application is appearing and disappearing from the screen.
when?
have you tried stepping through it in the debugger?
|
|
|
|
|
nothing is happening with debugger..its dll.....
appearing and disappearing....please give me some suggestion how can i launch the dll application from console client. Thanks a lot for comments. Thanks Sujan
|
|
|
|
|
|
That's hardly surprising since it's a console app trying to run a Windows DLL. you also seem to have a discrepancy between the definition of your runAppli() function and its implementation.
|
|
|
|
|
I am able to reduce the code to this much but still its crashing...
/////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"runAppli");
/*
Define the Function in the DLL for reuse. This is just prototyping the dll's function.
A mock of it. Use "stdcall" for maximum compatibility.
*/
typedef BOOL (* pICFUNC)(HINSTANCE, LPCSTR);
pICFUNC MyFunction=NULL;
///* The actual call to the function contained in the dll */
BOOL intMyReturnVal = MyFunction(HMODULE (hGetProcIDDLL), "runAppli");
/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) void runAppli()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
}
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
First point: please use <pre> tags around your code so that it is more readable.
Second point:
pICFUNC MyFunction=NULL;
BOOL intMyReturnVal = MyFunction(HMODULE (hGetProcIDDLL), "runAppli");
how do you expect a call to a NULL address not to crash?
|
|
|
|
|
Now I am writing like this.
int main(int argc, char **argv)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
typedef BOOL (* pICFUNC)(HINSTANCE, LPCSTR);
pICFUNC lpfnGetProcessID = (pICFUNC)GetProcAddress((HMODULE) hGetProcIDDLL,"runAppli");
///* The actual call to the function contained in the dll */
BOOL intMyReturnVal = lpfnGetProcessID((HMODULE) hGetProcIDDLL, "runAppli");
/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL __declspec(dllexport) runAppli(HWND hwnd, LPCSTR lpAppName)
{
// Only needed if resources are accessed
//AFX_MANAGE_STATE(AfxGetStaticModuleState());
::ShowWindow(hwnd, SW_SHOW);
UNREFERENCED_PARAMETER(lpAppName);
return true;
}
Somebody suggested this piece of code. But nothing is appearing with this. My mainwindow of the dll application is not coming.
Can u tell me whats wrong still in this. Thanks a lot for your help. Sujan
|
|
|
|
|
LoadLibrary[^] returns a HMODULE , not HINSTANCE . Alter the DLL loading to look like this:
HMODULE hDll = LoadLibrary(_T("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
Secondly, lose the cast like this (since you're now using the correct type above):
typedef BOOL (*PFUNC)(HINSTANCE, LPCSTR);
PFUNC pProc = (PFUNC)GetProcAddress(hDll, "runAppli");
Note also that I've added a _T (same as TEXT ) which you left out.
Now a guess at the cause of your problems: GetProcAddress[^] is failing because you're using the wrong function name by ignoring name mangling[^]. Change the DLL like this:
extern "C" BOOL __declspec(dllexport) runAppli(HWND hwnd, LPCSTR lpAppName)
Steve
modified 23-Oct-11 6:01am.
|
|
|
|
|
With this even its not happening stephen...
_T is not compiling so I have put only the text. its compiling
Now the code looks like this..
int main(int argc, char **argv)
{
/* get handle to dll */
HMODULE hDll = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
typedef BOOL (* ICFUNC)(HINSTANCE, LPCSTR);
ICFUNC lpfnGetProcessID = (ICFUNC)GetProcAddress(hDll, "runAppli");
///* The actual call to the function contained in the dll */
BOOL intMyReturnVal = lpfnGetProcessID(hDll, "runAppli");
/* Release the Dll */
FreeLibrary(hDll);
return 0;
}
extern "C" BOOL __declspec(dllexport) runAppli(HWND hwnd, LPCSTR lpAppName)
{
// Only needed if resources are accessed
//AFX_MANAGE_STATE(AfxGetStaticModuleState());
::ShowWindow(hwnd, SW_SHOW);
UNREFERENCED_PARAMETER(lpAppName);
return true;
}
|
|
|
|