
整体来看这个程序是用C#调用WIN32 API。
public const int SPI_SETWORKAREA = 47;
public const int SPI_GETWORKAREA = 48;
public const int SW_HIDE = 0x00;
public const int SW_SHOW = 0x0001;
public const int SPIF_UPDATEINIFILE = 0x01;
// 以上是变量定义,这些变量从命名方式看就是典型的WIN32 API。
[DllImport("coredlldll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpWindowName, string lpClassName);
[DllImport("coredlldll", EntryPoint = "ShowWindow")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("coredlldll", EntryPoint = "SystemParametersInfo")]
private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
// 以上3个方法是C#调用C函数的实现,DllImport是c#调用c或者C++代码的固定实现方式。
//以 [DllImport("coredlldll", EntryPoint = "FindWindow")]为例就是说使用coredlldll中的
//FindWindow函数。这些函数的具体功能可以在MSDN去查。 第一个方法的返回值是IntPtr //,这个是C#中的指针类型。
// 以下是一个函数
public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)
{
//调用C函数返回一个指针
IntPtr Hwnd = FindWindow("HHTaskBar", null);
// 判断指针是否为空
if (Hwnd == IntPtrZero) return false;
if (fullscreen)
{
//调用C函数
ShowWindow(Hwnd, SW_HIDE);
// ScreenPrimaryScreenBound这个玩意应该是其它代码中定义的,你贴的也不全
Rectangle rectFull = ScreenPrimaryScreenBounds;
//调用C函数
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get
//调用C函数
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//set
}
else
{
//调用C函数
ShowWindow(Hwnd, SW_SHOW);
//调用C函数
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);
}
return true;
}
所以你看看,其实很简单的东西。
你的dll应该是缺少导出函数吧。
我给你一个例子:
dll工程:
//dllh
extern "C" int __declspec(dllexport) add(int,int);
//dllcpp
#include <windowsh>
#include "dllh"
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int add(int a,int b)
{
return a+b;
}
exe工程:
//maincpp
#include <windowsh>
#include <iostream>
#include <tcharh>
using namespace std;
int main()
{
HINSTANCE hDll; //dll 句柄
hDll=LoadLibrary(TEXT("windll")); //加载dll
cout<<hDll<<endl;
typedef int( lpAddFun)(int,int); //宏定义函数指针类型
typedef int( lpSubFun)(int,int);
lpAddFun addFun; //函数指针
lpSubFun subFun;
int num;
if(hDll!=NULL) //判断dll加载是否成功
{
addFun=(lpAddFun)GetProcAddress(hDll,"add"); //获取想要引入的函数以及变量
subFun=(lpAddFun)GetProcAddress(hDll,"sub");
num=(int )GetProcAddress(hDll,"num");
if(num!=NULL)
{
printf("%d\n", num);
}
if(addFun!=NULL)
{
int result=addFun(3,2);
printf("3+2=%d\n",result);
}
if(subFun!=NULL)
{
int result=subFun(3,2);
printf("3-2=%d\n",result);
}
FreeLibrary(hDll); //卸载dll
}
return 0;
}
如果还是不明白,我给你发vs2008下的源代码。
如果是Windows
9x/Me系统中,屏蔽Ctrl+Alt+Del和各种任务开关键的方法是通过下面的方法实现的:
Const
SPI_SCREENSAVERRUNNING
=
97
Private
Declare
Function
SystemParametersInfo
Lib
"user32"
Alias
_
"SystemParametersInfoA"
(ByVal
uAction
As
Long,
ByVal
uParam
As
Long,
_
lpvParam
As
Any,
ByVal
fuWinIni
As
Long)
As
Long
Dim
pOld
As
Boolean
Call
SystemParametersInfo(SPI_SCREENSAVERRUNNING,
True,
pOld,
0)
但如果是NT/2000,则非常复杂,似乎连系统钩子都不能解决,而需要采取代码注入、远程线程等手段(这些手段也是一些病毒、木马喜欢采用的)。以下有篇文章可以参考:
>
using SystemRuntimeInteropServices;
//声明函数 SystemParametersInfo 两个重载版本
[DllImport("user32dll", EntryPoint = "SystemParametersInfo", CharSet = CharSetAuto)]
public static extern int GetSystemParametersInfo(int uAction, int uParam, out int lpvParam, int fuWinIni);
[DllImport("user32dll", EntryPoint = "SystemParametersInfo", CharSet = CharSetAuto)]
public static extern int SetSystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni)
参考:>
以上就是关于C#程序解读 DllImport原理全部的内容,包括:C#程序解读 DllImport原理、请问C++如何调用DLL文件里的东西、VB程序怎么实现禁止CTRL+ALT+DEL键等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)