Solved

Windows API

Posted on 2011-03-12
24
463 Views
Last Modified: 2012-06-21
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
Comment
Question by:Mohamed Abowarda
  • 10
  • 7
  • 2
  • +3
24 Comments
 
LVL 3

Assisted Solution

by:uanmi
uanmi earned 100 total points
ID: 35118945
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35118973
@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
 
LVL 3

Expert Comment

by:uanmi
ID: 35119049
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35119160
@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
 
LVL 3

Assisted Solution

by:uanmi
uanmi earned 100 total points
ID: 35119299
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
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 50 total points
ID: 35119814
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35121464
@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
 
LVL 3

Expert Comment

by:uanmi
ID: 35121508
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
 
LVL 7

Accepted Solution

by:
JimBeveridge earned 100 total points
ID: 35144195
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
 
LVL 3

Expert Comment

by:uanmi
ID: 35146660
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35148400
@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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Expert Comment

by:JimBeveridge
ID: 35148790
@uanmi: Please read prior responses before posting. You just repeated what I said.
0
 
LVL 3

Expert Comment

by:uanmi
ID: 35152500
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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 35891878
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35891967
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
 
LVL 3

Expert Comment

by:uanmi
ID: 35892026
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35892049
@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
 
LVL 3

Expert Comment

by:uanmi
ID: 35892137
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
 
LVL 12

Author Comment

by:Mohamed Abowarda
ID: 35892219
Can you post URL of that example?
Can that be done in C++ only?
0
 
LVL 3

Expert Comment

by:uanmi
ID: 35893733
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36492935
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
 
LVL 3

Expert Comment

by:uanmi
ID: 36492936
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now