Create a DLL by hand

Source: Internet
Author: User

I have search for release time about this aspect, but there is little about it, not let us share my work about do it.
The theory about DLL is simple, but to do it by hand it's not so easy.
1 first, the file:
Select. c
Bool apientry dllmain (handle hinst, DWORD ul_reason_being_called, lpvoid lpreserved)
{
Return 1;
Unreferenced_parameter (hinst );
Unreferenced_parameter (ul_reason_being_called );
Unreferenced_parameter (lpreserved );
}

Void MSG (){
MessageBox (null, "OK", "OK", mb_ OK );
}
2: Must give def file to get lib and then can create DLL.
Select. Def
Library select

Exports
MSG @ 2

3: compile, if you use Cl directly, error:
C:/DLL> Cl select. c
Microsoft (r) 32-bit C/C ++ Optimizing Compiler version 12.00.8804 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All rights reserved.

Select. c
Microsoft (r) incremental linker version 6.00.8447
Copyright (c) Microsoft Corp 1992-1998. All rights reserved.

/Out: select.exe
Select. OBJ
Link: Fatal error lnk1561: entry point must be defined
4: must use some option:
C:/DLL> Cl-C-W3-dcrtapi1 = _ cdecl-dcrtapi2 = _ cdecl-nologo-d_x86 _ = 1-d_winnt-d
_ Win32_winnt = 0x0400-d_win32_ie = 0x0300-dwinver = 0x0400-dwin32-d_win32-d_mt-
D_dll-MD-z7-od select. c
Then will create some file:
5: Then must create lib use Lib:
C:/DLL> Lib-machine: i386-Def: Select. Def select. obj-out: select
. Lib
Microsoft (r) Library Manager version 6.00.8447
Copyright (c) Microsoft Corp 1992-1998. All rights reserved.

Creating library select. lib and object select. Exp

6: Now can link and create DLL.
C:/DLL> link-Debug: Full-debugtype: CV/nodefaultlib/Incremental: No/PDB: NONE/R
Elasticsearch/nologo-entry: _ dllmaincrtstartup @ 12-DLL-base: 0x1c000000-out: SE
Lect. dll select. Exp select. OBJ msvcrt. Lib oldnames. Lib kernel32.lib ws2_32.l
IB mswsock. Lib advapi32.lib user32.lib gdi32.lib comdlg32.lib winspool. Lib
7: Now get select. dll file, can use it in C file, file is:
# Include "windows. H"
Void calldll ();
Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
// Todo: Place code here.
Calldll ();
: MessageBox (null, "Ssssss", "ssssssssss", 0 );
Return 0;
}
Int showinfo (){
: MessageBox (null, "info", "info", 0 );
Return 99;
}
Void calldll (){
Typedef void (winapi * func )();
Hinstance hmod;
Hmod =: loadlibrary ("Select. dll ");
If (hmod = NULL)
{
MessageBox (null, "fail", "fail", mb_ OK );
}
Func lpproc;
Lpproc = (func) getprocaddress (hmod, "MSG ");
If (lpproc! = (Func) null)
(* Lpproc )();
Else {
MessageBox (null, "can not find func", "can not find func", mb_ OK );
}
 
Freelibrary (hmod );
}

8: compile it and run:
CL usedll. cpp user32.lib
9: OK!
Addition:
So to use a DLL is not very easy by hand. Please enjoy it .!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.