⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
字号:
// demo.cpp : Defines the entry point for the console application.
//



#include <stdio.h> 
#include <windows.h> 
 
int main() 
 
{ 
   CHAR   inBuf[80]; 
   HKEY   hKey1, hKey2; 
   DWORD  dwDisposition; 
   LONG   lRetCode; 
   CHAR   szData[] = "USR:App Name\\Section1";
 
 
   WritePrivateProfileString ("Section1", 
	   "Hello", 
	   "fdfadsf", 
	   "appname.ini"); 
   WritePrivateProfileString ("Section2", 
	   "SecondKey", 
	   "By golly, it works.", 
	   "appname.ini"); 
   WritePrivateProfileSection ("Section1", 
	   "ThirdFdfsafey = Another dsfdafdasfsdafTF.", 
	   "appname.ini"); 
   WritePrivateProfileSection ("Section2", 
	   "ThirdFdfsafey = Another dsfdafdasfsdafTF.", 
	   "appname.ini");
   
   // Create the .ini file key. 
   lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE, 
       "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping\\appname.ini", 
       0, 
       NULL, 
       REG_OPTION_NON_VOLATILE, 
       KEY_WRITE, 
       NULL, 
       &hKey1, 
       &dwDisposition); 
 
   if (lRetCode != ERROR_SUCCESS)
   { 
      printf ("Error in creating appname.ini key\n"); 
      return (0) ; 
   } 
 
   // Set a section value 
   lRetCode = RegSetValueEx ( hKey1, 
                              "Section1", 
                              0, 
                              REG_SZ, 
                              (BYTE*)szData, 
                              sizeof(szData)); 
 
   if (lRetCode != ERROR_SUCCESS) 
   { 
      printf ("Error in setting Section1 value\n"); 
      // Close the key
      lRetCode = RegCloseKey( hKey1 );
      if( lRetCode != ERROR_SUCCESS )
      {
         printf("Error in RegCloseKey\n");
         return (0) ; 
      }
   } 
 
   // Create an App Name key 
   lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER, 
                               "App Name", 
                               0, 
                               NULL, 
                               REG_OPTION_NON_VOLATILE,
                               KEY_WRITE, 
                               NULL, 
                               &hKey2, 
                               &dwDisposition); 
 
   if (lRetCode != ERROR_SUCCESS) 
   { 
      printf ("Error in creating App Name key\n"); 

      // Close the key
      lRetCode = RegCloseKey( hKey2 );
      if( lRetCode != ERROR_SUCCESS )
      {
         printf("Error in RegCloseKey\n");
         return (0) ; 
      }
   } 
 
   // Force the system to read the mapping into shared memory 
   //    so that future invocations of the application will see it 
   //    without the user having to reboot the system 
   WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" ); 
 
   // Write some added values 
   WritePrivateProfileString ("Section1", 
                              "FirstKey", 
                              "It all worked out okay.", 
                              "appname.ini"); 
   WritePrivateProfileString ("Section1", 
                              "SecondKey", 
                              "By golly, it works.", 
                              "appname.ini"); 
   WritePrivateProfileSection ("Section1", 
                               "ThirdKey = Another Test.", 
                               "appname.ini"); 

   // Test 
   GetPrivateProfileString ("Section1", 
                            "FirstKey", 
                            "Bogus Value: Get didn't work", 
                            inBuf, 
                            80, 
                            "appname.ini"); 
   printf ("%s", inBuf); 
 
   // Close the keys
   lRetCode = RegCloseKey( hKey1 );
   if( lRetCode != ERROR_SUCCESS )
   {
      printf("Error in RegCloseKey\n");
      return(0);
   }

   lRetCode = RegCloseKey( hKey2 );
   if( lRetCode != ERROR_SUCCESS )
   {
      printf("Error in RegCloseKey\n");
      return(0);
   }
   
   return(1); 
} 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -