Ok, even though the dll is compiling fine now, when I call the function DS_Recieve from my dbp app, the app crashes silently, and leaves a crash log saying the problem is on line 7, which is the line where I put the DS_Recieve call.
Maybe someone (probably SSS
) can shed some light on this.
Here is the c++ code:
#include <helper.h>
#include <commands.h>
#include <stdafx.h>
#include <iostream.h>
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <globstruct.h>
#pragma comment(lib,"wsock32.lib")
//#include <stringtable.h>
#define NETWORK_ERROR -1
#define NETWORK_OK 0
#define MYCOMMAND __declspec ( dllexport )
SOCKET theSocket;
GlobStruct* Core;
GlobStruct* g_pGlob;
TPC_String AllocReturnString(TPC_String lpStrOld, TPC_ConstString lpStrReturn)
{
if (lpStrOld)
Core->CreateDeleteString((DWORD *)&lpStrOld, 0);
TPC_String lpStrNew = NULL;
int len = (int)strlen(lpStrReturn);
Core->CreateDeleteString((DWORD *)&lpStrNew, len + 1);
strcpy(lpStrNew, lpStrReturn);
return lpStrNew;
}
void ReceiveCoreDataPtr( void *cdp ){
g_pGlob = (GlobStruct *) cdp;
}
LPSTR dbpstring(LPSTR pOldString, LPSTR pstring)
{
if( pOldString )
g_pGlob->CreateDeleteString ( (DWORD *)&pOldString, 0 );
pOldString = NULL;
if( pstring )
{
DWORD dwSize = strlen( (LPSTR)pstring );
g_pGlob->CreateDeleteString ( (DWORD *)&pOldString, dwSize+1 );
strcpy(pOldString, (LPSTR)pstring);
}
return pOldString;
}
int WINAPI dllMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
}
MYCOMMAND int ReportError(int errorCode, const char *whichFunc)
{
char errorMsg[92]; // Declare a buffer to hold
// the generated error message
ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string
// The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
return errorCode;
//MessageBox(NULL, A2W(errorMsg), "socketIndication", MB_OK);
}
//closesocket(theSocket);
//WSACleanup();
MYCOMMAND char PrintText(LPSTR pString)
{
if(pString)
{
char dtmstg[255];
strcpy (dtmstg,"yippee");
return 0;
//MessageBox(NULL, pString, "", MB_OK);
}
}
MYCOMMAND int GetValue( void )
{
return 42;
}
MYCOMMAND int DS_Connect(LPSTR pString,int uport)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
// Store information about the server
LPHOSTENT hostEntry;
in_addr iaHost;
iaHost.s_addr = inet_addr(pString);
hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
if (!hostEntry) {
nret = WSAGetLastError();
int terrr;
terrr=ReportError(nret, "gethostbyname()"); // Report the error as before
WSACleanup();
return terrr;
}
// Create the socket
theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
if (theSocket == INVALID_SOCKET) {
nret = WSAGetLastError();
int terrr;
terrr=ReportError(nret, "socket()");
WSACleanup();
return terrr;
}
// Fill a SOCKADDR_IN struct with address information
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
// At this point, we've successfully retrieved vital information about the server,
// including its hostname, aliases, and IP addresses. Wait; how could a single
// computer have multiple addresses, and exactly what is the following line doing?
// See the explanation below.
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(uport); // Change to network-byte order and
// insert into port field
// Connect to the server
nret = connect(theSocket,
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));
if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
int terrr;
terrr=ReportError(nret, "connect()");
WSACleanup();
return terrr;
}
return 0;
}
MYCOMMAND int DS_Discon()
{
closesocket(theSocket);
WSACleanup();
return 0;
}
MYCOMMAND int DS_Send (LPSTR pString)
{
int nret;
char *buffer = new char[256]; // or on the heap
ZeroMemory(buffer, 256);
strcpy(buffer, pString);
nret = send(theSocket,
buffer,
strlen(buffer), // Note that this specifies the length of the string; not
// the size of the entire buffer
0); // Most often is zero, but see MSDN for other options
delete [] buffer; // If and only if the heap declaration was used
if (nret == SOCKET_ERROR) {
// Get a specific code
// Handle accordingly
nret = WSAGetLastError();
int terrr;
terrr=ReportError(nret, "connect()");
return terrr;
} else {
return 0;
// nret contains the number of bytes sent
}
}
MYCOMMAND DWORD DS_Receive(TPC_String strOld)
{
TPC_String result = AllocReturnString(strOld, "Hello world");
TPCReturn_String(result);
}
My string table entry is:
DS_Recieve[%S%?DS_Receive@@YAKPAD@Z%string
And my dbp code is:
a=DS_Connect ("127.0.0.1",9999)
print a
wait key
a=DS_Send ("dfdasfads")
print a
wait key
a$=DS_Recieve()
print a$
wait key
a=DS_Discon ()
print a
wait key
And the crashlog:
[CEXE]
m_dwRuntimeErrorDWORD=Internal Code:0
m_dwRuntimeErrorLineDWORD=7