|
in constructor's body, it's not, but in the constructor's initialization list, you can (and you must)
|
|
|
|
|
abhijit bhopale wrote: How to initilize const member variable in class,
in constructor also it;s not possible.
Along with other answers, you can use also enums...
enum{ AConstantValue = 100, AnotherConstantValue = 20 };
|
|
|
|
|
|
Hello everyone,
I am developing a DLL and I implement a class in the DLL. I am wondering in the two situations,
1. DLL is loaded by a single process multiple times
2. DLL is loaded a couple of times by multiple process
How much times will the constructor be invoked? I want to learn how objects inside a DLL is shared between different process. Is it possible to make multiple process share one single class (class is defined in DLL) object instance?
thanks in advance,
George
|
|
|
|
|
George_George wrote: 1. DLL is loaded by a single process multiple times
A single will load the dll only once.
George_George wrote: How much times will the constructor be invoked
If you have create only class, the costructor will never be called. You have to create an object of the class.
George_George wrote: Is it possible to make multiple process share one single class (class is defined in DLL) object instance?
We can share a data section in a dll among various process. see here[^]
|
|
|
|
|
Thanks nave,
Naveen.R wrote: A single will load the dll only once.
I mean for a specific process, if it invokes LoadLibrary multiple times without invoking unLoadLibrary, the DLL will be loaded multiple times or once one time?
Naveen.R wrote: If you have create only class, the costructor will never be called. You have to create an object of the class.
Sure, I create object (instance) of a class. I want to know if I make the class a singleton class, if the DLL is loaded by multiple processes, each process will have a separate copy of the singleton class object?
regards,
George
|
|
|
|
|
George_George wrote: the DLL will be loaded multiple times or once one time?
One time only.
George_George wrote: if the DLL is loaded by multiple processes, each process will have a separate copy of the singleton class object?
Offcourse multiple copy.
|
|
|
|
|
Thanks nave,
So, making a class singleton does not make an instance (object) shared between multiple process, right?
I have read through the article you recommended. And I learned that we can use #pragma data_seg to make a shared data segment. But I have not found any information about how to synchronize the acces of the data between multiple process (lockig?)? Any reference or samples?
regards,
George
|
|
|
|
|
May be you have to synchronize it using some mutex. Samples.. have to search
|
|
|
|
|
Thanks nave,
I noticed when using #pragma data_seg, all the shared data are global data. But when we use mutex, we need to use it in a function. I do not know how to use mutex outside a function and apply mutex to global data.
Any comments or ideas?
regards,
George
|
|
|
|
|
I hope you have a singleton class. create a member varibable say HANDLE m_hMutex in that class. In the constructor create the mutex( CreateMutex()) and destructor close it( CloseHandle()). Add some function to set/get the data to the shared segment. In that functions before getting/setting the datas wait for the mutex using the WaitForSingleObject() function and in the end of that function call ReleaseMutex().
|
|
|
|
|
Thanks nave,
I can understand that in your method, we can make function level synchronization through mutex, like get/set functions.
But where to put statement, like #pragma data_seg? I think you mean put #pragma data_seg inside get/set function? Could you show me a sample about where to put #pragma data_seg please?
regards,
George
|
|
|
|
|
Put them as global itself. you can access global varaible from the member functions isn't it?
#pragma data_seg("SHARED")<br />
char c;<br />
#pragma data_seg() <br />
<br />
<br />
SingleTon::Set()<br />
{<br />
c = '1';<br />
}<br />
<br />
SingleTon::get()<br />
{<br />
char temp = c;<br />
}
|
|
|
|
|
|
Hi all,
I'm desperately trying to create a dll-file in visual studio and use the file in Borland C++ Builder. Anyone with experience of that?
I've started vc++ and made a console application (dll) with exports. Then I've done the implib thing on it to get a lib-file for use in borland. Included the lib-file in borland, and the .h-file made in visual studio. But I always get this error (the class declared in the vc-dll is called CSHA):
[Linker Error] Unresolved external 'CSHA::CSHA()' referenced from C:\TMP\SHA\UNIT1.OBJ
Looks like Borland is unable to understand the lib-file or something. HELP!!
|
|
|
|
|
Hi,
Are you trying to use a class from that DLL?
Becaue in order to do so , you can't just create an instance of that class from your application,
because the constructor of that class is not being exported.
In order to use class from the DLL,your DLL should export a function which create that instance
and return a pointer to the created object.
For example,if you are trying to use a class called MyDllClass,then you need to export the following method:
MyDllClass *CreateMyDllClass()
{
return new MyDllClass;
}
Goodluck,
Eli
|
|
|
|
|
Eli, thanks for your (fast) reply!!
But I still got the same linking-error message.
Ok, this is what the cpp-file in vc++ looks like:
#include "stdafx.h"
#include "SHA.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
SHA_API int nSHA=0;
// This is an example of an exported function.
SHA_API int fnSHA(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see SHA.h for the class definition
CSHA::CSHA()
{
return;
}
CSHA *CSHA::New()
{
return new CSHA();
}
//--------------------------------------------------
This is the h-file (which I also use in my borland project):
#ifdef SHA_EXPORTS
#define SHA_API __declspec(dllexport)
#else
#define SHA_API __declspec(dllimport)
#endif
// This class is exported from the SHA.dll
class SHA_API CSHA {
public:
CSHA(void);
CSHA* New(void);
// TODO: add your methods here.
};
extern SHA_API int nSHA;
SHA_API int fnSHA(void);
//--------------------------------------------------
Borland cpp-file:
#include <vcl.h>
#pragma hdrstop
#include "sha.h"
CSHA* dll();
#pragma argsused
int main(int argc, char* argv[])
{
CSHA test;
CSHA* h = test.New();
return 0;
}
|
|
|
|
|
First,In the header of the DLL,put all exported functions and veriables inside the following block:
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
For example:
#ifdef __cplusplus
extern "C" {
#endif
class SHA_API CSHA {
public:
CSHA(void);
CSHA* New(void);
};
extern SHA_API int nSHA;
SHA_API int fnSHA(void);
#ifdef __cplusplus
}
#endif
In addition,your DLL solution should contain also a "*.def" file.
in that file you should add all the exported functions according to the following
example :
EXPORTS
fnSHA @1
Another exported function @2
Now,build your DLL again,and make sure that your application links to the correct dll/lib file.
Eli
|
|
|
|
|
I used the code you sent me, but I get compile error (C2470) You don't happen to have a working example of a cpp-file, h-file and def-file I could check?
And once again, thanks alot for you help!
/Martin
|
|
|
|
|
Hi Martin,
smsmfe wrote: I used the code you sent me, but I get compile error (C2470)
From MSDN :
Visual C++ Concepts: Building a C/C++ Program
Compiler Error C2470
'function' : looks like a function definition, but there is no formal parameter list; skipping apparent body
A function definition is missing its argument list. The following sample generates C2470:
int MyFunc {
};
int main(){
MyFunc();
}
This error may also be issued on a brace-enclosed initialization that is missing an =.
int arr[] { 1, 2, 3, 4, 5 };
Instead, you should use:
int arr[] = { 1, 2, 3, 4, 5 };
Can you post your source code after using my code?
Maybe I can see something you missed?
Regards,
Eli
|
|
|
|
|
h-file:
#ifdef __cplusplus
extern "C" {
#endif
// This class is exported from the SHA.dll
class SHA_API CSHA {
public:
CSHA(void);
CSHA* New(void);
// TODO: add your methods here.
};
extern SHA_API int nSHA;
SHA_API int fnSHA(void);
#ifdef __cplusplus
}
#endif
cpp-file:
// SHA.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "SHA.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
SHA_API int nSHA=0;
// This is an example of an exported function.
SHA_API int fnSHA(void)
{
return 42;
}
CSHA::CSHA()
{
return;
}
SHA_API CSHA* New()
{
return new CSHA();
}
|
|
|
|
|
I also can't call the function fnSHA()
Same err: [linker error] unresolved external 'fnSHA()' referenced from...
|
|
|
|
|
Hello Friends.
I need to compare two string in which one is of type WCHAR * and other is of type char * .
if anybody know how to do it. please tell me.
|
|
|
|
|
I think its better to convert char* to wchar* and then compare...
- NS -
|
|
|
|
|
Convert your char* to wchar* using MultiByteToWideChar
and then use wcscmp to compare these two strings...
|
|
|
|