如何使用结构标志值WriteFile [英] How to WriteFile with Struct Flag Values

查看:60
本文介绍了如何使用结构标志值WriteFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我有一个程序,我试图获取程序是否已被取消选中,检查或安装的标志值。目的是让该程序重新启动系统,但仍然可以在重启后从中断处继续。这个程序被削减到一个程序,同样不会让我试图解决这个问题。有人有任何提示吗?它创建了StateFile,但我不知道如何编写它来存储RSAFlag.State的值,并在必要时更改这些值。



更新:好的我有清理代码并移动一些东西。关于strlen,我该如何解决这个问题呢?我对这种更复杂的编码不太满意。我的原始程序允许用户在60个不同的程序之间进行选择以便安装,但它是基本的。问题是我们发现有几个程序需要重启,这就是为什么我必须开始深入研究更复杂的编码。另外,为了传递RSAFlag.State值,我还会调用什么?我认为这将是strlen但我正在尝试的一切都不起作用。通过在线研究找到解决方案似乎毫无意义,因为我能找到的任何东西都是关于控制台项目而不是Windows应用程序。



Hello All,

I have a program where I am trying to get flag values for whether a program has been unchecked, checked, or installed. The purpose is for this program to reboot the system but still be able to continue where it left off following the reboot. This program is cut down to one program for the same of not frusterating me trying to figure this out. Does anyone have any tips? It creates the StateFile but I am not sure how to code it to store those values of RSAFlag.State, and change those values when neccessary.

UPDATE: Ok I have cleaned up the code and moved some things around. So regarding strlen, how should I go about fixing that? I am not very good with this more complex coding. My origional program that would allow users to select between 60 different programs to install worked well but it was basic. The issue was that we found that a couple of the programs required a restart, that is why I have to start delving into more complex coding. Also, what would I call to in order to pass the RSAFlag.State value too? I thought it would be to strlen but everything I am trying is not working. Finding the solution through online research appears to be pointless as anything I can find regards console projects and not a windows application.

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{		
HANDLE StateFile = CreateFile("E:\\StateFile.txt", GENERIC_ALL, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE, SECURITY_ANONYMOUS);
	
char DataBuffer[MAX_PATH];
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
BOOL WriteStateFile = WriteFile(StateFile, DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);  
	 						   
     enum STATE{Unchecked, Checked, Installed};	
	struct STATE_FLAG	
	     {
    		STATE State;
    		STATE_FLAG(): State(Unchecked) {}
    	     };
    				      
    		STATE_FLAG RSAFlag;
						   	
bool checked = true;  
switch (message)                  /* handle the messages */
    {
    
     case WM_CTLCOLORSTATIC:
       {
    	HDC hdc = (HDC) wParam; 
    	SetTextColor(hdc, RGB(0,0,0));    
         SetBkMode (hdc, TRANSPARENT);

    	return (LRESULT)GetStockObject(NULL_BRUSH);   
       };        
        case WM_PAINT:  
             {          
               PAINTSTRUCT ps;    
               HDC hdc = BeginPaint( hwnd, &ps );  
                   TextOut( hdc, 20, 20, "Adobe Software:", 15);		
	          TextOut( hdc, 20, 70, "Microsoft Software:", 19);    	
	          TextOut( hdc, 20, 225, "Other:", 6);				
	          TextOut( hdc, 20, 415, "Model Software:", 15);			
	          TextOut( hdc, 20, 515, "OSR Software:", 13);			
               EndPaint( hwnd, &ps ); 						  
             };
             break;  
			 		
        case WM_CREATE:                                 
             
             {                              					
	       /*  Create SecureID Checkbox  */
                CreateWindowW(L"button", L"SecureID",
	       WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
	       20, 240, 188, 35,
	       hwnd, (HMENU) ID_RSA, NULL, NULL);
				
                /*  Create "Install" Button  */       
                CreateWindow(TEXT("button"), TEXT("Install"),
                WS_VISIBLE | WS_CHILD,
                650, 600, 80, 25,
                hwnd, (HMENU) ID_INSTALL, NULL, NULL);
                                   
                /*  Create "Cancel" Button  */
                CreateWindow(TEXT("Button"), TEXT("Cancel"),
                WS_VISIBLE | WS_CHILD,
                550, 600, 80, 25,
                hwnd, (HMENU) ID_CANCEL, NULL, NULL);
             }; 
             break;
                      
        case WM_COMMAND:     
              /*  If ID_CANCEL Is Pushed, Exit Program  */
              switch (LOWORD(wParam))
              {
                     case ID_CANCEL:
                          PostQuitMessage (0);
                          break;
              };   
                             
		switch (LOWORD(wParam))
              {      								 				  					 
	     /*  Identify If SecureID Is Checked  */
              case ID_RSA:
                 checked = IsDlgButtonChecked(hwnd, ID_RSA); 	   
		   if (checked) {
		       CheckDlgButton(hwnd, ID_RSA, BST_UNCHECKED);						 } else {
			    CheckDlgButton(hwnd, ID_RSA, BST_CHECKED);  						}								 											if(IsDlgButtonChecked(hwnd, ID_RSA)){
                                RSAFlag.State = Checked;								}	
	      break;						 			      }; 
			   
               switch (LOWORD(wParam))
               {  
                  case ID_INSTALL:
                                                                               	
                           	      
		        /*  Install RSA Secure ID  */							if(RSAFlag.State == Checked){
			CreateProcess(NULL,"C:\\Windows\\System32\\msiexec /i            E:\\programs\\RSA\\RSASecurIDToken400.msi /passive",
												        NULL,NULL,FALSE,								        CREATE_NEW_CONSOLE,
		        NULL,NULL,&si,&pi);						  				                                        WaitForSingleObject( pi.hProcess, INFINITE );					 						  					        RSAFlag.State = Installed;				                   }
																 
	                 /*  When program completes notify tech  */
		        CreateProcess(TEXT("E:\\Programs\\completion.bat"),
												        NULL,NULL,NULL,FALSE,							        CREATE_NEW_CONSOLE,							        NULL,NULL,&si,&pi);
WaitForSingleObject( pi.hProcess, INFINITE );					 							
	     break;                               
              };

推荐答案

你应该做几件事:



1.避免全局变量!在此代码段中,变量可以在本地定义。如果他们在其他任何地方使用,那可能已经(部分)出现了问题!如果没有,则没有理由在全球范围内定义它们。



2.不要调用函数来初始化全局变量!全局变量初始化在程序开始时完成,在调用 main()之前 - 无论编译器认为哪种顺序最有效。 您无法控制此类调用的顺序,并且很可能在调用函数时未定义程序的状态。因此无法保证功能能够带来预期的效果!此外,如果这些变量有一个全局的原因(即它们在不同的地方使用),那么你无论如何都无法确定它们处于什么状态 - 因此,仅用简单的值初始化它们就更好了。它们尚未正确初始化(例如NULL指针),并在正常程序执行期间执行函数调用,之后验证它尚未初始化。



特别说明:对于诸如 strlen 之类的简单函数,情况确实如此 - 并且您在初始化代码中使用该函数是一个实际错误!因此,后者(即后面的代码,不保证他们将在程序执行后期!)'初始化'灾难性地失败。 (虽然,根据您在初始化之前访问的内存状态,结果可能几乎任何事情)



3.修复代码

3.1声明 StateFile 以及其他全局变量作为局部变量。

3.2分配足够大的数据缓冲区

3.3在尝试写入之前用实际数据填充数据缓冲区

3.4关闭文件'完成
There are a couple of things you should do:

1. Avoid global variables! In this code snippet the variables may just as well defined locally. If they are used anywhere else, that may already be (part of) the reason for your problems! If not, there is no reason to define them globally.

2. Do not call functions to initialize global variables! Global variable initialization is done at the start of the program, before main() is called - in whatever order the compiler thinks is most efficient. You cannot control the order of such calls, and most likely the state of your program is undefined at the time the function is called. Therefore there is no guarantee the functions will deliver the expected results! Moreover, if there is a reason for these variables to be global (i. e. they are used in different places), then you can't be sure what state they are in anyway - it's therefore much better to only initialize them with trivial values that indicate they haven't been properly initialized yet (e. g. NULL-pointer), and do the function calls during the normal program execution, after verifying it hasn't been initialized before.

Special note: this is even true for trivial functions such as strlen - and your use of that function in initialization code is an actual error! As a consequence, the later (i. e. later in code, no guarantee they will be later in program execution!) 'intializations' catastrophically fail. (although, depending on the state of the memory that you access before intializing, the results may be just about anything)

3. Fix the code by
3.1 declaring StateFile and the other globals as local variables instead.
3.2 allocating a sufficiently large data buffer
3.3 filling the data buffer with actual data before trying to write it
3.4 Close the file when you're done


这篇关于如何使用结构标志值WriteFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆