|
Hum...The method I indicated works well for me.
Perhaps is there a mix between C and C++ decorations.
I've sent you a small sample, hope this help.
K.
Le temps se perd, "Si" n'existe pas
Tous les remords n'y changeront rien
Le temps se perd, "Si" n'existe pas
Donc à présent le choix reste mien
|
|
|
|
|
KaЯl,
Thanks very much for your sample code! I've got it working now. You never mentioned anything about dllimport! That was my problem. From my experimentation, I now believe the following:
- For global data (certainly for built-in data types and I presume this goes for global C++ objects as well), the DLL source code module must declare the data using __declspec(dllexport), BUT the application module must declare the data using __declspec(dllimport). This seems a tad annoying since the header file must look different for the DLL compile and the Application compile.
- For functions (and I presume this goes for class declarations as well), the DLL source code must likewise declare the function using __declspec(dllexport), BUT IN CONTRAST TO THE CASE FOR DATA the application can declare the function using __declspec(dllimport), __declspec(dllexport), or nothing at all!
Actually I just found the dllimport/dllexport descriptions in the MSDN that would seem to support my claims, though I find it somewhat cryptic -- the kind of description that makes sense only after you already know what it's trying to describe. Maybe I'm just mentally slow or lazy. I will have to read the entire section a time or two to see what I can learn.
Anyway, here is what I did for my source code to get this to work.
----- stupidDll.h -----
#ifndef STUPIDDLL_H
#define STUPIDDLL_H
#ifndef DllDecl
#define DllDecl __declspec(dllimport)
#endif
int DllDecl getStupidInt();
extern DllDecl int x;
#endif
-------------------------
----- stupidDll.cpp -----
#define DllDecl __declspec(dllexport)
#include "stupidDll.h"
int x = 8;
int foo = 7;
int getStupidInt() { return foo; }
-------------------------
----- stupidApp.cpp -----
#include "stupidDll.h"
#include "stdio.h"
int main(){
printf("%d %d\n", getStupidInt(), x);
return 0;
}
-------------------------
As you can see, my DllDecl macro normally expands to __declspec(dllimport); however, I predefined the DllDecl macro in StupidDll.cpp before the #include "StupidDll.h" to force it to instead expand to __declspec(dllexport).
Thanks again for your help. It was most helpful.
Matt
|
|
|
|
|
Matthew Busche wrote:
You never mentioned anything about dllimport
you asked how to export
You guessed well.
Generally, the technique is the following:
use one header file for both your DLL and your application. In it, define something like
#if _MY_DLL<br />
#define MYDDL_WHATEVER _declspec(dllexport)<br />
#else<br />
#define MYDDL_WHATEVER _declspec(dllimport)
Then, in the header, use:
extern MYDDL_WHATEVER int x;
In the settings of the DLL project, define the preprocessor definition MYDDL_WHATEVER. Et hop, it's done
You could also take a look to this article[^], it may contain useful info.
Matthew Busche wrote:
Thanks again for your help. It was most helpful.
My pleasure, and welcome on Codeproject!
K.
Le temps se perd, "Si" n'existe pas
Tous les remords n'y changeront rien
Le temps se perd, "Si" n'existe pas
Donc à présent le choix reste mien
|
|
|
|
|
use a def file, it's better for C exports.
Don't try it, just do it!
|
|
|
|
|
Alexander,
As I said in my email, I am trying to export two C++ objects -- not a simple integer. Do you still recommend a defs file? In addition, I have a few C++ base-class declarations and a few global C++ functions that I am also exporting. I would expect putting these in a defs file would not be as straight forward. Can you mix a defs file for data with __declspec for classes and functions all within the same DLL?
Thanks much,
Matt
P.S., what is the significance of the superscript M. in your login?
|
|
|
|
|
.
hello , i have used a class CPicture (obtained from this site itself) to load a jpeg image from the resource..now the problem is if i have to reload another image in the same sapce i have to unload the previous image....how do i do that????
Any kind of help is appreciated.
Thanking you.
Satadru
|
|
|
|
|
i hope u don#t want to unload the picture u just override the picture by calling the loadpicture function and replace the piecture in the same place.
Shanmuga Sundar.V
|
|
|
|
|
MY DLL CODE : CDBMgrClass is a generic class got added to the
DLL workspace for IGenericDBManager Interface.
I am calling my interface standard method i.e. STDMETHODIMP CGenericDBManager::Fetch(BSTR bstrSELECTSQLString, _Recordset **pFetchRecordset, BSTR *bstrFetchErrDesc, LPDISPATCH *ppVal)
from my stub in VC++(MFC). But the stub returns the OxThe thread 0x388 has exited with code 0 (0x0).
First-chance exception in GH_DBM_COMDLL_Stub_MFC_EXE.exe: 0xC0000005: Access Violation.
First-chance exception in GH_DBM_COMDLL_Stub_MFC_EXE.exe: 0xC0000005: Access Violation.
Below the DLL code : The STDMETHODIMP calls internally CDBMgrClass::ExecuteFetchQuery()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : ExecuteFetchQuery()
// Description : It does the EXECUTE of the FETCH SQL COMMAND
// Return Value : _RecordsetPtr
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_RecordsetPtr CDBMgrClass::ExecuteFetchQuery(char* sqlstring)
{
HRESULT hr = S_OK;
try
{
IsConnected();
m_csDatabaseOpLock.Lock();
TESTHR(m_pCommand.CreateInstance(__uuidof(Command)));
TESTHR(m_pRecordset.CreateInstance(__uuidof(Recordset)));
TESTHR(m_pParameter.CreateInstance(__uuidof(Parameter)));
m_pCommand->ActiveConnection = m_pConnObj;
m_pCommand->CommandText = sqlstring;
m_pCommand->CommandType = adCmdText;
m_pRecordset = m_pCommand->Execute(NULL, NULL, adCmdText);
m_pConnObj->Close();
}
catch(CException exception)
{
// Handle the exception here.
// "exception" contains information about the MFC DB exception.
TCHAR szCause[255];
EString strFormatted;
exception.GetErrorMessage(szCause, 255);
strFormatted = _T("The Error: ");
strFormatted += szCause;
OutputDebugString(strFormatted.c_str());
m_bIsConnected = FAILURE;
m_pRecordset = NULL;
}
catch(_com_error &e)
{
hr = e.Error();
_bstr_t bstrError = e.Description();
OutputDebugString(bstrError);
m_bIsConnected = FAILURE;
m_pRecordset = NULL;
}
catch(...)
{
}
m_csDatabaseOpLock.Unlock();
return m_pRecordset;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : Fetch
// Description : Interface Public Method
// Input : /*[in]*/BSTR bstrSELECTSQLString
// Output : /*[out]*/_Recordset **pFetchRecordset, /*[out]*/BSTR *bstrFetchErrDesc,/*[out]*/LPDISPATCH *ppVal
// Return Value : S_OK or E_FAIL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STDMETHODIMP CGenericDBManager::Fetch(BSTR bstrSELECTSQLString, _Recordset **pFetchRecordset, BSTR *bstrFetchErrDesc, LPDISPATCH *ppVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
USES_CONVERSION;
CoInitialize(NULL);
HRESULT hr = S_OK;
CDBMgrClass* pDBMgrClass = NULL;
_RecordsetPtr m_pRecord_Set(__uuidof(Recordset));
m_pRecord_Set = NULL;
pFetchRecordset = NULL;
try
{
pDBMgrClass = new CDBMgrClass();
if(pDBMgrClass)
{
_bstr_t _bstrFetchQuery(bstrSELECTSQLString);
m_pRecord_Set = pDBMgrClass->ExecuteFetchQuery(_bstrFetchQuery.operator char*());
if(m_pRecord_Set)
*pFetchRecordset = m_pRecord_Set;
m_pRecord_Set->QueryInterface(IID_IDispatch,(void**) &ppVal);
m_pRecord_Set->Close();
}
else
{
EString strFormatted;
strFormatted = _T("\nGHDER Database Fetch Error\n");
_bstr_t _bstrFetchErrorString(_T(strFormatted.c_str()));
*bstrFetchErrDesc = _bstrFetchErrorString.operator wchar_t*();
delete pDBMgrClass;
}
// Clean up objects before exit
bstrFetchErrDesc = NULL;
delete pDBMgrClass;
}
catch(CException exception)
{
// Handle the exception here.
// "exception" contains information about the MFC DB exception.
TCHAR szCause[255];
EString strFormatted;
exception.GetErrorMessage(szCause, 255);
strFormatted = _T("The Error: ");
strFormatted += szCause;
_bstr_t _bstrFetchErrorString(_T(strFormatted.c_str()));
*bstrFetchErrDesc = _bstrFetchErrorString.operator wchar_t*();
delete pDBMgrClass;
hr = E_FAIL;
}
catch ( _com_error &e )
{
_bstr_t bstrError = e.Description();
_variant_t tErr(e.Error());
_bstr_t tMsg("IGenericDBManager::Fetch::=" );
tMsg += "::Error::";
tMsg += (_bstr_t) tErr;
*bstrFetchErrDesc = tMsg.operator wchar_t*();
delete pDBMgrClass;
hr = E_FAIL;
}
catch(...)
{
}
CoUninitialize();
return hr;
}
///////////////////////////////////////////////////////////////////////
STUB CALLER>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//////////////////////////////////////////////////////////////////////
void CGH_DBM_COMDLL_Stub_MFC_EXEDlg::OnFetch()
{
HRESULT hr = S_OK;
USES_CONVERSION;
BSTR *pbstrFetchErrString = NULL;
_RecordsetPtr pRecordset = NULL;
IGenericDBManager *pGenericDBManager = NULL;
BSTR bstrSELECTSQLQuery;
//FORM THE SQL QUERY FOR ANY TABLE
_bstr_t _bstrSELECTSQLString = (_T("SELECT * FROM GHDR1TS_HAZARDOUS_COMMODITY"));
bstrSELECTSQLQuery = _bstrSELECTSQLString.operator wchar_t*();
//END QUERY
pRecordset.CreateInstance(__uuidof(Recordset));
//Initialize the COM library
CoInitialize(NULL);
//Instantiate the COM object for the interface i.e. IDBM_ComDll
hr = CoCreateInstance(CLSID_GenericDBManager, NULL, CLSCTX_INPROC_SERVER,
IID_IGenericDBManager, (void **) &pGenericDBManager);
if (FAILED(hr))
{
MessageBox("\nIGenericDBManager Interface not instantiated\n", NULL, MB_OK);
return;
}
else
{
MessageBox("\nIGenericDBManager Interface instantiated\n", NULL, MB_OK);
VARIANT* pvntDB_Connect_Status = NULL;
BSTR* pbstrConnectErrString = NULL;
hr = pGenericDBManager->Connect((VARIANT*) &pvntDB_Connect_Status, pbstrConnectErrString);
if (SUCCEEDED(hr))
{
hr = pGenericDBManager->Fetch(bstrSELECTSQLQuery, &pRecordset, pbstrFetchErrString);
if (FAILED(hr))
{
MessageBox("\nIGenericDBManager::Fetch() failed\n", NULL, MB_OK);
return;
}
else
{
VARIANT* pConnection = NULL;
VARIANT* pTable = NULL;
_bstr_t _bstrTable(_T("GHDR1TS_HAZARDOUS_COMMODITY"));
_variant_t _vtTable(_bstrTable.operator const wchar_t *());
hr = pRecordset->get_ActiveConnection((VARIANT*) &pConnection);
_variant_t _vtConnection(*pConnection,true);
hr = pRecordset->Open( _vtTable.Detach(), _vtConnection.Detach(),adOpenKeyset,adLockOptimistic,adCmdText);
if(pRecordset != NULL)
{
int nRecordCount = pRecordset->RecordCount;
int nMaxRecords = pRecordset->MaxRecords;
int nFieldCount = pRecordset->Fields->Count;
MessageBox((LPCTSTR) nRecordCount, NULL, MB_OK );
MessageBox((LPCTSTR) nMaxRecords, NULL, MB_OK );
pRecordset->MoveFirst();
for (int i = 0; i< nRecordCount; nRecordCount++)
{
for(int j=0; j< nFieldCount; j++)
{
MessageBox((LPCTSTR)pRecordset->Fields->GetItem("IMO")->Value.bstrVal, NULL, MB_OK);
}
pRecordset->MoveNext();
}
pRecordset->Close();
}
else
{
MessageBox("NULL RECORDSET", NULL, MB_OK);
pRecordset->Close();
}
}
}
else
{
MessageBox("CONNECT FAILURE", NULL, MB_OK);
}
}
//Uninitialize the COM library
CoUninitialize();
}
/
|
|
|
|
|
This line's very suspicious:
*bstrFetchErrDesc = _bstrFetchErrorString.operator wchar_t*(); You're trying to assign a non-prefixed string to a variable that the caller expects will contain a BSTR. I think you're trying to stop your local _bstr_t object from freeing the encapsulated BSTR when it goes out of scope. The _bstr_t class does not have any way to do this (in Visual C++ 6.0); I recommend using ATL's CComBSTR instead, using its Detach method once you've assigned to *bstrFetchErrDesc .
You should also be aware that MFC overrides the global operator new , replacing it with a version that throws a CMemoryException when the allocation fails. It does not return NULL .
|
|
|
|
|
Hi there
I would just like to know how do i write a programme
that would take an error code as a argument and display
the error message thats associated with the error code
Example: ERROR CODE 00300
Print : Bad IP Checksum
How and in what should i store the list of errors
How do i iterate\scan thru the list get the error code
and print the error message.
Can i store both in one file?
|
|
|
|
|
Have you looked at FormatMessage() ?
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
could u please use a topic that describes the problem?
Don't try it, just do it!
|
|
|
|
|
Hi,
I know it is possible to hook a function which is exported in a dll, but I was wondering if it is possible to hook a function in an exe?
thanx
Don't think you are, know you are...
|
|
|
|
|
Do you have the source of that exe?
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Yes, I do have the source of that exe.
What I would like to do is make some sort of program that hooks several functions of the specific exe so I can extract internal information.
We have lots of version of this exe and we can't put lots of debugging information in them...
Don't think you are, know you are...
|
|
|
|
|
-You can export these functions from the exe
or
-Call a common function that is exported into the dll and giving you the amount of info you need
Or Make own your own debugging thread and check the CreateRemoteThread family API
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
The problem is that I can't modify the sources of the original exe.
That's why I was wondering if it is possible the hook an 'internal' function, given that you know the name of it (if this is even relevant).
Don't think you are, know you are...
|
|
|
|
|
In fact it is, but its a delicate process.
Saying you have several versions of the exe, so a reverse engineering method (patching and calling a code from an exe) is somehow difficult or even not feasible if you dont want to modify the code
So a CreateRemoteThread is a better way to deal with this problem, with a Stack handling method in order to see what function is being called so you dump its parameters
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
I'll have a look at the CreateRemoteThread thingy...
Thanx
Don't think you are, know you are...
|
|
|
|
|
everything is possible!
tell me what's ur software's purpose and i'll tell u how to do it.
Don't try it, just do it!
|
|
|
|
|
Hi!
I'm have some problems with atoi. My simple program reading numbers from a file into a buffer. The numbers represent the time of the day with microseconds precision.
Ex. 124831123456
124831234567
Then will I subtract them but it will not work? Can someone help me?
int line;
int t, t1, t2;
char buffer[ROW][Buffer_Size];
ifstream inFile;
char inputFilename;
for(int k=0; k<20; k++){
inFile.getline(buffer[k], TIME_SIZE);
}
for(int x=1; x<20; x++){
t2 = atoi(buffer[x]);
t1 = atoi(buffer[x-1]);
t = t2 -t1;
cout << "t is equal to " <
|
|
|
|
|
124831123456 is too big to fit in a int
try atof and fill it into a float or check strtol
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
|
Hi,
What is the difference between BOOL and bool??
Best Regards,
A.Ilamparithi
|
|
|
|
|
This topic is already discussed.
Quick info:
BOOL is a int ( integer value from 0 to 65535 )
bool is a boolean ( true false )
but BOOL can be used as bool ( BOOL bBoolean = TRUE or BOOL bBoolean = FALSE )
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|