string i c++
Jeg har nu prøvet at få mit string argument i en c++ dll til at virke, dog uden held. Nogen der kan se hvad der er galt? (markerer med fed der hvor jeg har ændret koden og den derfor desværre stoppede med at virke..)DLL-koden, bliver kaldt fra et andet program via "SQLProcedureSendOrders" og der hentes en text streng fra en stored procedure som er af formatet char(100), output parameteren i SP hedder @order.
#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)
#include <iostream>
#include <string>
using namespace std;
static string SQLexecProcedure(char *bBroker, char *nprc );
// call as a procedure passing parameters
MT4_EXPFUNC string __stdcall SQLProcedureSendOrders(char *Broker,char *sProcedure)
{
string Ret = SQLexecProcedure( Broker, sProcedure );
return(Ret);
}
#include "stdafx.h"
#include <stdio.h>
#import "C:\Program Files\Common Files\System\ado\msado20.tlb" \
rename("EOF","ADOEOF") rename("BOF","ADOBOF")
using namespace ADODB;
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
// procedure call method
string SQLexecProcedure(char *bBroker, char *nprc )
{
HRESULT hr = S_OK;
_CommandPtr pcmd = NULL;
_ConnectionPtr pConnection = NULL;
_bstr_t strMessage, strAuthorID;
::CoInitialize(NULL);
string codRet = "-1";
try {
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
hr = pConnection->Open("dsn=xxxx;", "xxx", "xxxx", adConnectUnspecified);
pConnection->CursorLocation = adUseClient;
TESTHR(pcmd.CreateInstance(__uuidof(Command)));
pcmd->CommandText = nprc; // procedure name
pcmd->CommandType = adCmdStoredProc;
_ParameterPtr pParm1 = pcmd->CreateParameter( _bstr_t("@order"), adChar, adParamOutput, 0 ,0 );
pcmd->Parameters->Append( pParm1 );
_ParameterPtr Par1 = pcmd->CreateParameter("@psBroker",adChar, adParamInput, strlen(bBroker) ,bBroker );
pcmd->Parameters->Append(Par1);
pcmd->ActiveConnection = pConnection;
int hr = pcmd->Execute( 0, 0, adCmdStoredProc );
if( FAILED(hr) )
{
codRet = "-1";
}
else
{
pParm1 = pcmd->Parameters->GetItem(_bstr_t("@order")); // obtain from the procedure
codRet = pParm1->GetValue();
}
}
catch(_com_error ) {
//
// if necessary, process the execution error
//
codRet = "-1";
}
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
::CoUninitialize();
return (codRet);
}