• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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.

  • 6
  • 4
  • 2
1 Solution
SFSAuthor Commented:
Edited text of question.
SFSAuthor Commented:
Adjusted points to 150
1. Use shared memory. Search MSDN for "shared memory". I have soure lying around, but I'll have to dig around for it. Lemme know if you need it.

2. Use __declspec( dllexport ) to export functions.

3. If you have a DWORD as the thread ID, pass a pointer to it:

DWORD threadID;
GetWindowThreadProcessID( hWnd, &threadID );
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

SFSAuthor Commented:
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;

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?

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?

SFSAuthor Commented:
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...
SFSAuthor Commented:
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


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  


Could you find some things wrong?
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.
SFSAuthor Commented:
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?


Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now