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

Windows API

Windows API DLL files such as user32.dll was written in C++, however I'm not sure how it was made.

If I want to write DLL file exactly the same as user32.dll in C++, how can I do that?

More clarification:
How can I write a function such as GetForegroundWindow() as it made in user32.dll?
0
Mohamed Abowarda
Asked:
Mohamed Abowarda
  • 10
  • 7
  • 2
  • +3
4 Solutions
 
uanmiCommented:
yes, you can do this. You would need a C++ compiler such as that found in Microsoft Visual Studio. I would also recommend that you look at a code obfuscator so that your code cannot be de-compiled.

You should look for Windows API books such as the classic by Petzold.

Look at this example on how to create a C++ DLL

http://www.icynorth.com/development/createdlltutorial.html

Microsoft provides the free Microsoft Visual C++ Express - start with this

There are many other free C++ compilers

gnu C++ with Cygwin will make DLLs

regards, Mark

0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@uanmi:
When I want to get the active window handle I use GetForegroundWindow API in C++, my question was how can I create my own DLL that get the active window handle same like GetForegroundWindow() without having to use user32.dll?
0
 
uanmiCommented:
Hi, There are several ways to get the active window, but the functions all call user32.dll. Such as

BOOL WINAPI EnumWindows

DWORD WINAPI GetWindowThreadProcessId

http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx

I suppose you could hook into the message queue and identify the active window from messages in the queue. Examples of hooks can be found in early win32 text - such as petzold and others.

Look at this

http://msdn.microsoft.com/en-us/library/ms644959(v=vs.85).aspx

regards,
Mark

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@uanmi: GetForegoundWindow() API was example, however I want to know how the first API DLL file was created without using API

The question is about the original codes that created API.
0
 
uanmiCommented:
ok, you got me - the question keeps changing as we go along.

The API Dlls use a hook (normally written using MASM) into the message queue and other core routines to get the system, application and other states.

If you wish to build your own core API this is a considerable task. For customisation you can hook into a number of the core aspects of Windows, I recommend you read about hooks and MASM.

Look at this

http://msdn.microsoft.com/en-us/library/ms644960(v=vs.85).aspx

and I recommend you read this if you cannot get books on hooks

http://www.woodmann.com/fravia/fragas1.htm

regards, mark
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Regarding creating DLL's refer this;

http://www.flipcode.com/archives/Creating_And_Using_DLLs.shtml
http://logix4u.net/Programming/vc++/A_Tutorial_on_creating_DLLs_with_VC++.html
http://msdn.microsoft.com/en-us/windows/hardware/gg487379

Regarding creating user32.dll. The user32 dll calls other dlls. Open C:\Windows\system32\user32.dll using dependency walker.(http://www.dependencywalker.com/). You will be able to see the lower layer of the dlls which are loaded by the user32.dll which calls the device driver etc.

I am not quite clear about your question yet.Are you trying to create a wrapper function for GetForegroundWindow() ?
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@DeepuAbrahamK: No, I only want to know how the API was originally created.

Definitely, the core functions was created without using other APIs, so I'm trying to know how to create API libraries exactly like user32.dll, kernel32.dll, etc... without using them.
0
 
uanmiCommented:
The API are based upon a combination of (1) dependencies of other Dlls and (2) assembler hooks to the underlying OS. Windows is built to interact with the computer hardware through several layers including the facility to manage queues for hardware devices. The windows message queue is an important queue that is abstracted by the layers including the eventual function calls you get in the dlls.

I recommend you find this book and read it.

Windows Internals, by Russonovich, Solomon, and Ionescu - less about APIs and more about how Windows works under the hood

Here is a list of books that should be good reading if you can find copies

http://www.winprog.org/tutorial/references.html

regards,
mark
0
 
JimBeveridgeCommented:
Outside of USER32, there's no such thing as a foreground window. USER32 defines the meaning of "foreground window" because it maintains all of the data structures related to that. USER32 is almost completely responsible for managing the desktop, message queue, window hierarchy, etc. Therefore, without USER32, you don't have a foreground window.

If you want to see how all of this works, look at the Wine project. It's a faithful re-creation of most aspects of Windows and you'll be able to see exactly how a function like GetForegroundWindow() might work.
0
 
uanmiCommented:
Wine is a good example of how an OS is created. It will essentially show you what has been put earlier, that the user32.dll hooks to the OS message queues and uses assembler to get to the lower rings of the OS.

How is your reading going?

regards, Mark
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@uanmi: I need more examples on how can I create CORE DLL files, so I can create full library exactly like windows API library without having to use them.
0
 
JimBeveridgeCommented:
@uanmi: Please read prior responses before posting. You just repeated what I said.
0
 
uanmiCommented:
Hi Medo,

you cannot make core dlls in windows, this is something that cannot be done. You can make dlls that hook into some of the message queues and hardware but not all.

Let me know what you have been able to read - like petzold, inwodws internals

Also, look for a book by Jeffrey Richter called advanced windows - this is a great book to have
also, Windows via C/C++, Fifth Edition by Jeffrey Richter and Christophe Nasarre

remember that after vista there is a need to make some changes when doing hooks.
regards, Mark
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
2 more qs.
1.Do u know how to create & use dll in C++?If u don't know, u have to ask this first.
2. If u know then u can simply ask for how to implement GetForegroundWindow() without help of user32.dll.
wht exactly u want.
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
I want to create CORE dll as user32.dll, I am not talking about normal dll, I am talking about core dll that can have a function like GetForegroundWindow() or GetWindowText() without using user32.dll or any other DLLs.
0
 
uanmiCommented:
Hi Medo,

I think the answers that have been provided sum up the situation. The OS has certain limitations regarding what users can do and how. I have explained how to hook into message queues and to submit to a message queue. You can use assembler to carry out activities at the lower area of OS.

regards, Mark
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
@uanmi: That's only in the case that I want to get window information, what If I want to create a DLL that connect to another machine without using API? and what If I want to write to the hard drive?

I want to create my own API exactly like Windows API.

Lets assume that Windows API doesn't exists, now I want to create them.
0
 
uanmiCommented:
Hi Medo,

you're talking about writing your own OS. An example of WINE was given before. There are other examples of small OS available - look for real time operating systems.

You can work with most devices using assembler routines that can hook into the core of windows without using other DLLs. Check out MASM and examples.

regards, Mark
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
Can you post URL of that example?
Can that be done in C++ only?
0
 
uanmiCommented:
0
 
evilrixSenior Software Engineer (Avast)Commented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
uanmiCommented:
hi,

I believe the answer that I provided was spot on. The person who asked the question has gone round in circles, yet the answer has been correct all along. I request that the points be allocated appropriately.

regards, Mark
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 10
  • 7
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now