Link to home
Start Free TrialLog in
Avatar of SFS
SFS

asked on

two simple questions

1:There is a globle variable "A" in a .DLL ;
In my .EXE, I also have a int variable "B" that have a meaningful content.Now,I load the .dll and I want to put the B's content into A;like B=A;How can I achieve?
If I write B=A in my .EXE,there will be unsolved error for "A".
How can I transport variable content between .dll and .exe?

2.Is .DEF necesaary for a .DLL which I want to export a few functions from .Are there other ways?


3.I have get a handle of a window and I want to get the threadID of it.I use GetWindowThreadProcessId() but I dont know what should be put in the second param.


Really hope somebody expert can help me.
Thanks.

Avatar of SFS
SFS

ASKER

Edited text of question.
Avatar of SFS

ASKER

Adjusted points to 150
ASKER CERTIFIED SOLUTION
Avatar of rocket96
rocket96

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SFS

ASKER

Thank you,rocket96.
1.The so called "share memory" is meant to be shared between the .exe that load the .dll and the .dll ,right?
would you please give me a simple but concrete example ,just like make A=B(A in .dll and B in .exe).

BTW:I'm very urgent for the answer.I 'm writing a critical and very important homework and I will send in it tomorrow that is deadline!:)

2.I add the __declspec( dllexport) before my function FUNC which  in my .dll and I compile it successfully.Then,in the .exe ,I use LoadLibrary() first,it succeed. When I use Loadprocaddress() in my .exe,It return NULL,wrong! Should I add something else to my .exe? I import it correctly if I use a .DEF file to export that function.  

3.Thank you,I succeed.
But I'm so curious that ,originally,I use these words to do but failed.
LPDWORD pthreadid;
GetWindowThreadProcessId(hwndnote,*pthreadid);

Now I change as you said, but what the difference?



1. Sorry, didn't read your question properly. If you want B=A where B is in the DLL and A is in the exe, then just have a function which the exe can call into the DLL to set B. I can't explain more becuase its a homework question and we're not allowed to answer homework questions.

2. When you use __declspec( dllexport ), you should get an import library which your exe links against. There's no need to call LoadProcAddress() unless you don't have an import library.

3. You declared a pointer which was not allocated. And then you dereferenced an unallocated pointer. Thats not legal.
1:OK,that's enough,thanks
So,you misunderstand and what is "shared memory",when should I use? would you tell me?(This is not homework question:)


2.So,if I want to use LoadProcAddress() to import a function that in a .dll.I must write a .DEF?

3.Thanks.
BTW:I have send in my homework as I said last time.
I fail and I don't know how to make A=B.How I declare B(in .dll) in .exe?

Avatar of SFS

ASKER

Woo,what's the problem why Babyworship say that?

If B is in a DLL, and A is in an exe, and you want to do:
1. A = B
Export a function in your dll, say GetB(), which returns the value of B. Then your exe code can have:
A = GetB();
Where GetB() is exported from the dll.

2. B = A
This is a little trickier. You can use "shared memory" or memory mapped files, but these techniques are more advanced. Alternatively, you can export a function in your DLL called SetB( argument ); and call it in your EXE like this:

SetB( A );

To use LoadProcAddress, you don't need a def. You should be able to do it with __declspec( dllexport ). I was able to get this to work...
Avatar of SFS

ASKER

1...>>You can use "shared memory" or memory mapped files

I think those two tech are all used to share memory between different processes.But a .dll is in the same process of the .exe.Why you said  I can make use of them to make B=A?(B in .dll,A in .exe)?

2...I still failed even I add the __declspec(dllexport);But originally I succeed if I add a .def,here is my code.

In .exe:

hinstDLL = LoadLibrary((LPCTSTR) "xx.dll");  
hp = (HOOKPROC)GetProcAddress(hinstDLL, "Proc");

The "hp" return NULL,if I compile .dll with a .def

LIBRARY xx
EXPORTS
Proc

It is ok ...

In .dll:
__declspec(dllexport) LRESULT CALLBACK Proc(int nCode, WPARAM  wParam, LPARAM  lParam );
//declaration,I add what you said.
   
LRESULT CALLBACK Proc(int nCode, WPARAM  wParam,  
                                LPARAM  lParam  
                            )
{
                  
.........................................

}
//implementation


Could you find some things wrong?
Thanks...
1. If you use memory mapped files or shared memory, then you can say B=A or A=B with either variables in an exe or a dll.

2. If the return value from GetProcAddress() is NULL, use GetLastError() to check the last error.
Avatar of SFS

ASKER

2.I use the GetLastError() and It return 126,it's  ERROR_MOD_NOT_FOUND :
The specified module could not be found.

The loadlibrary is ok,why?