Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, I have created a list box in Win32 API like this
C++
// list1 is HWND variable
list1 = CreateWindow(L"LISTBOX", NULL, WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,10,150,330,200, hWnd, (HMENU)ID_LISTBOX,hInst, NULL);

It does created successfully. To avoid confusion I've used several cplusplus files. I have created a class in the separate header file and defined it in a separate cpp file. By using the header file I've called the member functions using objects in my main cpp file.

But the problem is sir one of member function should add strings to the list. I know it can be achieved by SendMessage function when everything is in the same cpp file. Since my class is in the different .cpp file I can't do this.

What I have tried:

1. Made list1 as a global variable by adding my variable before the forward declarations of functions included in my code module.

When I do like this I am getting "uninitialized variable error"

2. Did moved the variable to a new header file and made it accessible to both cpp files

I got "Multiple declarations of variable" error

3. I need of something like the variable to be used and defined somewhere else so I made use of storage classes ("extern" in this case) but when using extern I get a strange error in the object file. I am quiet sure object file can't be read by humans so I did completely stopped researching aboout this error and the usage of extern variables.

C++
Severity	Code	Description	Project	File	Line
Error	LNK2005	"struct HWND__ * list1" (?list1@@3PAUHWND__@@A) already defined in Portable Devices Manager.obj	 Portable Devices Manager	G:\Portable Devices Manager\Portable Devices Manager\ShortCutVirusKiller.obj	


Kindly help me with this sir please
Posted
Updated 16-Mar-16 23:54pm

1 solution

  • declare your variable as extern in a header file, e.g.

C
// mylist.h
extern HWND  hwndMyList;


  • define it in just one source file, e.g.

C
// mylist.cpp
// ...
HWND hwndMyList;
// ...


  • include the header file in all the other sources needing the variable

C
// foo.cpp
#include "mylist.h"
//...
hwndMyList = CreateWindow(/*...*/);
//... 
 
Share this answer
 
Comments
[no name] 17-Mar-16 6:33am    
Thank you sir for your kind help and time. I defined my variable in multiple source files like defining "functions". I think that was the error.
Thank you once again sir
CPallini 17-Mar-16 6:54am    
You are welcome.
Arthur V. Ratz 21-Mar-16 3:13am    
+5.
CPallini 21-Mar-16 4:19am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900