|
First make sure you've covered the requirements for a regular DLL
that uses MFC (taken from the docs):
1) Compiled with _USRDLL defined, and if the DLL links to MFC dynamically,
_AFXDLL must be defined.
2) The DLL must instantiate a CWinApp-derived class.
3) The DLL uses the DllMain provided by MFC. Don't add your own.
Place all DLL-specific initialization code in the InitInstance
member function and termination code in ExitInstance as in a normal
MFC application.
In the dll you can use AfxBeginThread() to create worker threads.
If you don't know how to actually use a DLL, you may want to study:
DLLs[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks Mark.
But When I did it as you said,it does not seem to work at all!
can you give me some example code?
Never Change My Goal!
|
|
|
|
|
Not working how?
Please list any compiler errors, linker errors, runtime exceptions, etc.
To start with, are you able to successfully call a function in your DLL?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
there is no compiler errors,linker errors..
the dll loads successfully,and I can call the function in the dll too.
and here is the funtion in the dll:
UINT threadproc(LPVOID lparam)
{
::MessageBox(NULL,"call the function succeefully","info",MB_OK);
return 0;
}
extern "C" void __declspec(dllexport) dllext()
{
::MessageBox(NULL,"call the function succeefully in dllext()","info",MB_OK);
CWinThread*pThread=AfxBeginThread(threadproc,NULL,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED ,NULL);
pThread->ResumeThread();
}
and here is the function in a normal MFC application:
void CMainFrame::OnBeginthread()
{
typedef void (*Pdllfun)();
HINSTANCE hdll=LoadLibrary("thread.dll");
if(hdll==NULL)
::MessageBox(NULL,"can not laod the dll","",MB_OK);
Pdllfun pdllfun=NULL;
pdllfun=(Pdllfun)::GetProcAddress(hdll,"dllext");
if(pdllfun==NULL)
::MessageBox(NULL,"can not laod the dll funion","",MB_OK);
pdllfun();
FreeLibrary(hdll);
}
if I do not call the pThread->ResumeThread() function which in the dll,everything will be ok.
but once I call the pThread->ResumeThread() function,the main application will crash.
and the errors it returns to me is:
0x00fd102d instruction references "0x00fd102d" memory can not be written
Never Change My Goal!
modified on Monday, November 10, 2008 3:27 AM
|
|
|
|
|
The fact that you're using LoadLibrary to load the dll at run time makes a HUGE difference to your question.
In this case, I wouldn't go any near MFC for the DLL at all, just plain vanilla Win32.
I can see one gaping problem though - after you've called resume thread, you return from dllext(), and the promptly call FreeLibrary (). That will (as the name says...) free the library. As you only loaded it once, that will also unload it from your memory. Then windows does a context switch (or could have done at any time - or even simultaneously) to your thread. Which tries to run code that doesn't exist anymore...
Imagine Wile E Coyote running off a cliff, running into mid air for a few seconds, looking down... that's what you've done to your code.
Iain.
|
|
|
|
|
thanks Iain.I have solved the problem.
The fact that you are right!
void CMainFrame::OnBeginthread()
{
typedef void (*Pdllfun)();
HINSTANCE hdll=LoadLibrary("thread.dll");
if(hdll==NULL)
::MessageBox(NULL,"can not laod the dll","",MB_OK);
Pdllfun pdllfun=NULL;
pdllfun=(Pdllfun)::GetProcAddress(hdll,"dllext");
if(pdllfun==NULL)
::MessageBox(NULL,"can not laod the dll funion","",MB_OK);
pdllfun();
::MessageBox(NULL,"hold the dll","",MB_OK);
FreeLibrary(hdll);
}
so..thanks Iain again!!
Never Change My Goal!
|
|
|
|
|
Iain Clarke wrote: Imagine Wile E Coyote running off a cliff, running into mid air for a few seconds, looking down... that's what you've done to your code.
Excellent!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Iain Clarke wrote: I wouldn't go any near MFC for the DLL at all, just plain vanilla Win32.
If he's truly properly implemented a "regular DLL linked to MFC" as
described here[^]. there should ne no problems.
Probably some thread synchronization issues in the near future for kim_wu...
Thanks for handling the remainder of this thread!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I got two questions.
1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?
int* intPtr
OR
int *intPtr; IMO, the second one is more clear when we declare something like this.
int *intPtr1,intPtr2; 2 - Is it a good practice to append const with member function that doesn't modify any member variables?
int foo::sampleMethod() const{
} I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations?
Any help would be great.
|
|
|
|
|
I personally prefer the int* intPtr; But I declare a single variable in a line. This way I can add a comment for each variable I use.
I do add const to each member function that doesn't modify member variables. The problem is you can almost never predict how the class is going to be used at a later time. Once I had to add const to loads of member function for about 10-20 classes. Also, I am not sure, but in certain cases adding const might help compiler to produce more optimized code.
-Saurabh
|
|
|
|
|
Saurabh.Garg wrote: But I declare a single variable in a line. This way I can add a comment for each variable I use.
Saurabh.Garg wrote: The problem is you can almost never predict how the class is going to be used at a later time.
Good points.
Saurabh.Garg wrote: I am not sure, but in certain cases adding const might help compiler to produce more optimized code.
Do you have any articles/books which explains this?
Thanks for your help
|
|
|
|
|
you have an interview, don't you ?
Christian Flutcher wrote: 1 - Which is the preferred way of using the pointer symbol ?
for me, I like to declare every variable separatedly, and I don't like to mix declarations of different types.
this way, I prefer using int* pi; syntax because I see immediately that pi is of type int*.
Christian Flutcher wrote: 2 - Is it a good practice to append const with member function that doesn't modify any member variables?
definitely, yes !
because the one who write the class is not always the one who will use it, a class definition has to be clear, and has to mean what the class is designed for.
When you have an accessor which just does a "get", the it's obviously not modifying the object, and should be declared as const.
Christian Flutcher wrote: I understand why const member functions exists. But if we are not planning to make the class object as constant,
humm, it seems to me that you don't fully understand the thing.
making a function member constant doesn't mean every function members have to be consts, and it doesn't mean either that the object will be used as a constant.
If you'd like to say that the object is used as a constant, then you would have to do like this :
<font color="blue">class</font> foo { ... };
<font color="blue">const</font> foo f(<font color="green"></font>);
|
|
|
|
|
toxcct wrote: you have an interview, don't you ?
No I don't have. I just finished reading "Thinking in C++" and about to start a project in C++. So thought of getting some expert advice on those points.
Thanks for you help. It was really helpful.
|
|
|
|
|
1/ I normally do:
int *pInt;
2/ the question you ask is why I never mix those. And if the variable it at all significant, it gets its own line. I use long variable names - thanks to intellitext they're just as easy to type, and a lot easier to read. Only loop vars get bunched together. int i,j,k;
3/ I try to use const on member functions that *shouldn't* modify the state, not whether I think I might do so. It gives me the freedom to change my mind in one direction, and makes it harder to code mistakes in the other.
Iain.
|
|
|
|
|
Perfect ! Thanks for the help Iain.
|
|
|
|
|
|
Can you elaborate? Format what like this?
-Saurabh
|
|
|
|
|
123456789.00
to
123,456,789.00
Just add a comma every 3 digits at the left of the decimal point.
|
|
|
|
|
Here is the correct method for displaying numerics in English_USA.1252
double dMoney = 0123456789.0123456789;
char szOldCppMoney[MAX_PATH] = {0};
char szNewCppMoney[MAX_PATH] = {0};
sprintf(szOldCppMoney,"%2.2f",dMoney);
LCID lc = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
GetNumberFormat(lc,0,szOldCppMoney,NULL,szNewCppMoney,ARRAYSIZE(szNewCppMoney));
Output will be: "123,456,789.00"
Best Wishes,
-David Delaune
|
|
|
|
|
See StrFormatByteSize if you want to use of bytes/kilobytes,...
|
|
|
|
|
I use ShellExecute() to execute some operations on a ".txt" file. But it just executed a few of them and the others returned an Error.
These operations did not work:
-------------------------------------
1- Properties
2- Delete
3- Rename
.
.
.
Any help?
Thank you masters!
|
|
|
|
|
Not all verbs are available to all files and folders.
As the docs state:
"Generally, the actions available from an object's shortcut
menu are available verbs."
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes I know! But the verbs Rename, Delete and Properties are common to all objectes. Aren't they?
|
|
|
|
|
if you want to delete/rename a file, why don't you use the dedicated API ?
|
|
|
|
|
I poked around my registry and tried a bunch of right-clicks
on files of different types...I couldn't find any type that supported
any of those.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|