当前位置:编程学习 > C#/ASP.NET >>

dll文件创建问题```急求帮助

#include "stdafx.h"
#include <psapi.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#include<windows.h>
#include<stdio.h>
#include <shlobj.h>
#include <SHELLAPI.H>
#pragma comment(lib,"psapi.lib") 
#pragma comment(lib,"msvcrt.lib")

#pragma comment(linker, "/SECTION:.text,REW") 
#pragma comment(linker, "/MERGE:.data=.text") 
#pragma comment(linker, "/MERGE:.rdata=.text")

void myTestFun();
DWORD ProcessToPID(const char *ProcessName, DWORD aPid[1024]);            
int AddPrivilege(const char *Name);

BOOL APIENTRY DllMain( HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)

{

     switch (ul_reason_for_call)

     {

     case DLL_PROCESS_ATTACH:
         myTestFun();
         //break;

     case DLL_THREAD_ATTACH:
         //myTestFun();
         break;

     case DLL_THREAD_DETACH:
         //myTestFun();
         break;

     case DLL_PROCESS_DETACH:
         //myTestFun();
         break;

     }

    return TRUE;

}

 

#ifdef _MANAGED

#pragma managed(pop)

#endif

 
UINT_PTR hTimer=0;
 void CALLBACK TimerProc(HWND  hwnd,UINT uMsg,UINT_PTR  idEvent,DWORD  dwTime)  
  {

  if(hTimer==idEvent)
  {
  ShellExecute(NULL,"open","http://www.126.com",NULL,NULL,SW_SHOW);
  //MessageBoxEx(NULL,"测试文本","测试标题",MB_OK,0);
  }

  }

     

void myTestFun()

{
  const   char   DESTPROC[19] = "explorer.exe";     
  DWORD  pid =ProcessToPID(DESTPROC, NULL);

  if (pid=0)   //返回为0就是没有 
  {
      MSG msg;
  PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE);
      hTimer=SetTimer(NULL,0,10000,TimerProc);
  
  while (GetMessage(&msg,NULL,0,0))
  {
      TranslateMessage(&msg);;
      DispatchMessage(&msg);
  }
}
}

DWORD ProcessToPID(const char *ProcessName, DWORD aPid[1024])
{
    
    typedef BOOL (CALLBACK* EnumProcessesType)(DWORD *,DWORD,DWORD *);
    typedef BOOL (CALLBACK* EnumProcessModulesType)(HANDLE,HMODULE *,DWORD,LPDWORD);
    typedef DWORD (CALLBACK* GetModuleBaseNameType)(HANDLE, HMODULE, LPTSTR, DWORD);
    
    EnumProcessesType EnumProcesses;
    EnumProcessModulesType EnumProcessModules;
    GetModuleBaseNameType GetModuleBaseName;    

    HMODULE hmPsapi = GetModuleHandle("psapi.dll");    
    if (hmPsapi == NULL)
    {
        if ((hmPsapi = LoadLibrary("psapi.dll")) == NULL)
        {          
            return 0;
        }        
    }
    EnumProcesses = (EnumProcessesType)GetProcAddress(hmPsapi, "EnumProcesses");
    EnumProcessModules = (EnumProcessModulesType)GetProcAddress(hmPsapi, "EnumProcessModules");
    GetModuleBaseName = (GetModuleBaseNameType)GetProcAddress(hmPsapi, "GetModuleBaseNameA");   

    if (!(EnumProcesses &&
            EnumProcessModules &&
            GetModuleBaseName))
    {
        FreeLibrary(hmPsapi);
        
        #ifdef _DEBUG
            printf("GetProcAddress() error : %d\n", GetLastError());
        #endif
        return 0;
    }
    
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i , j;
    HANDLE hProcess;
    HMODULE hMod;
    char szProcessName[MAX_PATH] = "UnknownProcess";


    // 计算目前有多少进程, aProcesses[]用来存放有效的进程PID
    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
    {
        #ifdef _DEBUG
            printf("EnumProcesses() error : %d\n", GetLastError());
        #endif
        
        FreeLibrary(hmPsapi);
        return 0;
    }

    cProcesses = cbNeeded / sizeof(DWORD);

    // 按有效的PID遍历所有的进程
    for ( i = 0, j = 0; i < cProcesses; i++ )
    {
        // 打开特定PID的进程
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                                PROCESS_VM_READ,
                                FALSE,
                                aProcesses[i]);
        // 取得特定PID的进程名
        if ( hProcess )
        {
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
            {
                GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName));

                //将取得的进程名与输入的进程名比较,如相同则返回进程PID
                if(!stricmp(szProcessName, ProcessName))
                {
                    CloseHandle( hProcess );

                    //如果接收缓冲区有效,就依次填入pid,否则立即返回
                    if (aPid != NULL)
                    {
                        aPid[j++] = aProcesses[i];
                    }
                    else
                    {
                        FreeLibrary(hmPsapi);
                      
                        return aProcesses[i];
                    }

                }
            }
        }
    }

    CloseHandle( hProcess );

    if (aPid != NULL)
    {
        FreeLibrary(hmPsapi);
       
        return aPid[0];
    }
        
    FreeLibrary(hmPsapi);
    return 0;

  int AddPrivilege(const char *Name)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tp;
    LUID Luid;

    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
        &hToken))
    {
         return 1;
    }

    if (!LookupPrivilegeValue(NULL,Name,&Luid))
    {
         return 1;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    tp.Privileges[0].Luid = Luid;

    if (!AdjustTokenPrivileges(hToken,
        0,
        &tp,
        sizeof(TOKEN_PRIVILEGES),
        NULL,
        NULL))
    {      
        return 1;
    }

    return 0;
}


程序要求:查看软件运行情况 每1000毫秒查看一下软件运行情况 如果没有启动运行这个dll文件就自动启动软件 如果软件启动就再等1000毫秒查看 循环

ShellExecute(NULL,"open","http://www.126.com",NULL,NULL,SW_SHOW);

这句好像也有问题 打开望着那没出什么错误 但是如果换成了软件 就不能自动启动这个软件 请高手帮忙 

现在这个dll实现了每隔10000毫秒就打开126网站  需要实现的如上 定时监视软件的运行情况


希望高手帮忙#15....急`!!!~~急!!~~~急!!!!!!!!!!!!~~~~~~~~`
--------------------编程问答-------------------- 把C++的路也搬到这里来问,看来不是一般的迷路。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,