none
[SOLVED] Getting an error in xxx_writer,its not returning success value ????? RRS feed

  • Question

  • Hello friends,

    I have created an application that is calling my driver and application is sending string using WriteFile and it is calling to TST_Write... But, the driver return type is FALSE

    My application write code is as--

    void Write_Data(HANDLE frmlLed){
    	
    	///*DWORD dwDataWritten = 0;*/
    //	LPTSTR *data = "Hello Partap";
    	//TCHAR sBuffer[26] = "Hello Partap";
    	char sBuffer[] = "Hello Partap";
    	DWORD nBytesToWrite = (DWORD)strlen(sBuffer);
    	DWORD nBytesWritten = 0;
    	DWORD result = 0;
    	//DWORD rtrn = 0;
    	
    	result = WriteFile(frmlLed, sBuffer, nBytesToWrite, &nBytesWritten, NULL);//On success,this fxn invokes the driver xxx_write fxn
    	if(result){
    		RETAILMSG(1, (TEXT("Data written successfully...")));
    		//rtrn = 1;
    	}
    	else{
    		RETAILMSG(1, (TEXT("Error in writting...")));
    
    	}
    
    	RETAILMSG(1, (TEXT("Application Written END...")));
    	//return rtrn;
    }

    and my driver TST_Write code is as ---

    #define BUFFERSIZE	256

    typedef struct _DRIVERSHELL_CONTEXT
    {
                    DWORD Instance;
    TCHAR *StrngVal;
    TCHAR *rwBuffer;
                    
    } DRIVERSHELL_CONTEXT;

    DWORD TST_Write(DWORD hOpenContext, LPCVOID pBuffer, DWORD Count, HANDLE hAsyncRef) { DWORD lp = 0; DWORD rVal = 0; //DWORD lcl_pBuffer = (DWORD)pBuffer; DRIVERSHELL_CONTEXT *pDriverContext; RETAILMSG(TRUE,(TEXT("TST_Write(): START\r\n"))); rVal = min(BUFFERSIZE, Count); RETAILMSG(TRUE,(TEXT("TST_Write(): rVal is %d\r\n",rVal))); wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, rVal); RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %s\r\n",wcsncpy))); RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %d\r\n",GetLastError()))); RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %s\r\n",pDriverContext->rwBuffer))); RETAILMSG(TRUE,(TEXT("TST_Write(): END\r\n"))); return rVal; }


    And my retail messages are as ----

     18649 PID:400002 TID:44500d2 TST_Write(): START
      18649 PID:400002 TID:44500d2 TST_Write(): rVal is 4194306
      18650 PID:400002 TID:44500d2 TST_Write(): WCSNCP is ýÿ
      18653 PID:400002 TID:44500d2 TST_Write(): Written string is------- 4194306
      18653 PID:400002 TID:44500d2 Exception 'Data Abort' (0x4): Thread-Id=044500d2(pth=9e4a60c0), Proc-Id=00400002(pprc=828bf700) 'NK.EXE', VM-active=047800d2(pprc=9e4a5e30) 'TestDriver.exe'
      18654 PID:400002 TID:44500d2 PC=8003c12c(kernel.dll+0x0000d12c) RA=8005f21c(kernel.dll+0x0003021c) SP=ac8bfb3c, BVA=00400002
      18655 PID:400002 TID:44500d2 Exception 'Raised Exception' (0x116): Thread-Id=044500d2(pth=9e4a60c0), Proc-Id=00400002(pprc=828bf700) 'NK.EXE', VM-active=047800d2(pprc=9e4a5e30) 'TestDriver.exe'
      18656 PID:400002 TID:44500d2 PC=eff6eef8(k.coredll.dll+0x0001eef8) RA=800442ac(kernel.dll+0x000152ac) SP=ac8bf250, BVA=ffffffff
      18657 PID:47800d2 TID:44500d2 Error in writting...
      18657 PID:47800d2 TID:44500d2 Application Written END...



    Wednesday, April 2, 2014 6:50 AM

Answers

  • Hello Friends, 

    The problem is self solved... The mistake was here

    rVal = min(BUFFERSIZE, Count);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): rVal is %d\r\n",rVal)));
    
    	wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, rVal);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %s\r\n",wcsncpy)));
    	
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Error is------- %s %d \r\n",GetLastError(),GetLastError())));
    		//RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %s\r\n",pDriverContext->rwBuffer)));
    	

    And this is the correct one code with correct syntax --- 

    wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, Count);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): Count is %d\r\n"),Count));
    	RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %S\r\n"),pDriverContext->rwBuffer));
    	
    	
    		//RETAILMSG(TRUE,(TEXT("TST_Write(): Error is-------%d \r\n",GetLastError())));
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %S\r\n"),pDriverContext->rwBuffer));

    The error was in RETAILMSG debug syntax and string format specifier .... 
     I am happy that its solved by me with all your support....

    Thanks for warm support ...


    Friday, April 4, 2014 5:09 AM

All replies

  • Hi Pratap,

    When you want to communicate with driver from application you need to use createfile api first to get the handle of respective driver.

    Refer the following link for more information,its a example on UART you use the same for your driver

    http://chrisryu.wordpress.com/2009/03/27/wince-sample-code-for-serial-port/

    Using GetlastError api you can find the nature of fail for the API.

    Hope this will help you.

    With Regards

    Veena

    Wednesday, April 2, 2014 9:48 AM
  • Thanks for warm reply,

    I have already created all these things, that's why application after opening file writing some thing

    Please have a look in the following link for more info about the functionality before write processssss-

    http://social.msdn.microsoft.com/Forums/en-US/7600d323-4fa5-4ae8-ac48-709c0f01dbf7/solvedpb-console-displays-retailmsg-as-errorin-creating-file?forum=winembplatdev

    Wednesday, April 2, 2014 10:09 AM
  • Hi,

    I checked the link provided by you,

    if(hLed = INVALID_HANDLE_VALUE){
    
    		RETAILMSG(1, (TEXT("Error in creating file...")));
    		
    	}

    Hope in the above checking you are using "==" instead of single "="

    What is the index of TST driver?

    With Regards

    Veena

    Wednesday, April 2, 2014 10:24 AM
  • Hello,

    This is my working share code -

    App

    TESTDriver.cpp

    // DriverTest.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "partap.h"
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	RETAILMSG(1, (TEXT("In Main...")));
    	Create_File();
    	return 0;
    }
    
    
    void Create_File()
    {
    
    	RETAILMSG(1, (TEXT("In Create File function...................\n")));
    	HANDLE hLed = CreateFile(L"TST1:",GENERIC_READ|GENERIC_WRITE,\
    		0, NULL, OPEN_EXISTING|CREATE_NEW,\
    		0, NULL);
    
    	if(INVALID_HANDLE_VALUE == hLed){
    
    		RETAILMSG(1, (TEXT("Error in creating file...")));
    		
    	}
    	else{
    		RETAILMSG(1, (TEXT("File created successfully...")));
    		Write_Data(hLed);
    		Read_Data(hLed);
    		
    	}
    	CloseHandle(hLed);
    }
    
    void Write_Data(HANDLE frmlLed){
    	
    	///*DWORD dwDataWritten = 0;*/
    //	LPTSTR *data = "Hello Partap";
    	//TCHAR sBuffer[26] = "Hello Partap";
    	char sBuffer[] = "Hello Partap";
    	DWORD nBytesToWrite = (DWORD)strlen(sBuffer);
    	DWORD nBytesWritten = 0;
    	DWORD result = 0;
    	//DWORD rtrn = 0;
    	
    	result = WriteFile(frmlLed, sBuffer, nBytesToWrite, &nBytesWritten, NULL);//On success,this fxn invokes the driver xxx_write fxn
    	if(result){
    		RETAILMSG(1, (TEXT("Data written successfully...")));
    		//rtrn = 1;
    	}
    	else{
    		RETAILMSG(1, (TEXT("Error in writting...")));
    
    	}
    
    	RETAILMSG(1, (TEXT("Application Written END...")));
    	//return rtrn;
    }
    
    void Read_Data(HANDLE frmlLed){
    	
    	////DWORD dwDataToRead = 0;
    	DWORD dwDataRead = 0;
    	////DWORD result = 0;
    	char sBuffer[28];
    	DWORD sSize= strlen(sBuffer);
    	DWORD result = 0;
    
    	//DWORD rtrn = 0;
    	
    	result = ReadFile(frmlLed, sBuffer, sSize, &dwDataRead, NULL);
    	if(result > 0){
    		RETAILMSG(1, (TEXT("Data read successfully...")));
    		//rtrn = 1;
    	}
    	else{
    		RETAILMSG(1, (TEXT("Error in reading...")));
    
    	}
    	RETAILMSG(1, (TEXT("Application read END...")));
    	//return rtrn;
    }
    
    
    
    

    Driver TEST.cpp 

    // Test1.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    #include "Test.h"
    #include "winbase.h"
    #include "Devload.h"
    #include "Winreg.h"
    
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
                         )
    {
    	RETAILMSG(TRUE,(TEXT("TST_DLL(): Entry Point\r\n")));
    
    
    	RETAILMSG(TRUE,(TEXT("TST_DLL(): Exit Point\r\n")));
        return TRUE;
    }
    
    
    DWORD TST_Init(
      LPCTSTR regPath,
      DWORD dwBusContext
    )
    {
    	HKEY	hKey;
    	DWORD	result;
    	//DWORD Type	=	REG_DWORD;
    	DWORD Type = REG_SZ;
    	TCHAR *MemAlloc = NULL;
    	DWORD Data;
    	DWORD NoBytes;
    	DWORD DataSize	=	sizeof(DWORD);
    	DWORD lcl_error;
    ////	DWORD Type;
    ////
    ////
    ////DWORD cbValue = sizeof(DWORD);
    ////UCHAR RegDataCompare[4]={0};
    ////
    ////DWORD Data=0;
    	DRIVERSHELL_CONTEXT *pDriverContext;
    
    
    	RETAILMSG(TRUE,(TEXT("TST_Init(): START\r\n")));
    
    		////pDriverContext = LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		if(!pDriverContext == NULL)
    		{
    			hKey = OpenDeviceKey((LPCTSTR)regPath);// This fxn opens the device registery key,
    				
    			if(!hKey)
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to open registery\r\n")));
    				return FALSE;
    				exit;
    			}
    			else
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Registery key open successfully\r\n")));
    				//result = RegQueryValueEx(hKey, TEXT("Index"), NULL, &Type, (LPBYTE)&Data, &DataSize);
    
    
    				result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, NULL, &NoBytes);
    
    				if(NoBytes > 0)
    				{
    					MemAlloc = (TCHAR *)malloc(NoBytes);
    					if(MemAlloc != NULL)
    					{
    						result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, LPBYTE(MemAlloc), &NoBytes);
    						if(result == ERROR_SUCCESS)
    						{
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Registry values read successfully.\r\n"))); 
    							pDriverContext->StrngVal = MemAlloc;
    
    							RETAILMSG(TRUE, (TEXT("TEST_Init(): Registery value is -- %s"),pDriverContext->StrngVal));
    							RegCloseKey(hKey);
    
    						}
    						else
    						{
    							lcl_error = GetLastError();
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): -- %d --%s \r\n"),lcl_error, lcl_error));
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in reading registry values.\r\n")));
    							return FALSE;
    							exit;
    						}
    
    					}
    					else
    					{
    						RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to allocate memory to bytes\r\n")));
    						exit;
    					}
    				}
    					
    				///*result = RegQueryValueEx(hKey, TEXT("Order"), NULL, &Type, (LPBYTE)&Data, &DataSize);*/
    				///*/*result = RegQueryValueEx(hKey,TEXT("Dll"),NULL,&Type,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/*/
    				///*result = RegQueryValueEx(hKey,TEXT("Order"),NULL,NULL,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/
    					//memcpy(&Data,RegDataCompare,sizeof(DWORD));
    							}								// with pointer to the registery
    		}
    		else
    		{
    			RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in allocating memory.\r\n")));
    			return FALSE;
    			exit;
    		}
    
    	
    	
    	RETAILMSG(TRUE,(TEXT("TST_Init(): END\r\n")));
    	return (DWORD)pDriverContext;
    	//return TRUE;
    
    }
    
    BOOL TST_Deinit(
      DWORD hDeviceContext 
    )
    {
    	DRIVERSHELL_CONTEXT *pDriverContext;
    	RETAILMSG(TRUE,(TEXT("TST_Deinit(): START\r\n")));
    
    	pDriverContext = (DRIVERSHELL_CONTEXT *)hDeviceContext;
    	if(pDriverContext)
    	{
    		while(pDriverContext != NULL)
    			LocalFree(pDriverContext);
    		if(pDriverContext == NULL)
    		{
    			RETAILMSG(TRUE,(TEXT("TST_Deinit(): Memory de-allocated successfully!\r\n")));
    		}
    	}
    
    	
    	
    	
    	RETAILMSG(TRUE,(TEXT("TST_Deinit(): END\r\n")));
    	
    	return TRUE;
    }
     
    
    DWORD TST_Open(
      DWORD hDeviceContext,
      DWORD AccessCode,
      DWORD ShareMode 
    )
    {
    	RETAILMSG(TRUE,(TEXT("TST_Open(): START\r\n")));
    
    	
    	RETAILMSG(TRUE,(TEXT("TST_Open(): END\r\n")));
    
    	return hDeviceContext;
    	
    }
    
    BOOL TST_Close(
      DWORD hOpenContext 
    )
    {
    		RETAILMSG(TRUE,(TEXT("TST_Close(): START\r\n")));
    		
    		
    
    		RETAILMSG(TRUE,(TEXT("TST_Close(): END\r\n")));
    
    		return TRUE;
    }
    DWORD TST_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
    {
    	RETAILMSG(TRUE,(TEXT("TST_Read(): START\r\n")));
    		
    		
    
    	RETAILMSG(TRUE,(TEXT("TST_Read(): END\r\n")));
        
    	return Count;
    }
    
    DWORD TST_Write(DWORD hOpenContext, LPCVOID pBuffer, DWORD Count, HANDLE hAsyncRef)
    {
    		
    	DWORD lp = 0;
    	DWORD rVal = 0;
    	
    	//DWORD lcl_pBuffer = (DWORD)pBuffer;
    	DRIVERSHELL_CONTEXT *pDriverContext;
    	///////*if(hOpenContext == NULL)
    	//////{
    	//////	RETAILMSG(TRUE,(TEXT("TST_Write(): hOpenContext is NULL\r\n")));
    	//////}
    	//////else
    	//////{
    	//////	RETAILMSG(TRUE,(TEXT("TST_Write(): hOpenContext Instance value is-- %d \r\n",hOpenContext->)));
    	//////}*/
    
    	RETAILMSG(TRUE,(TEXT("TST_Write(): START\r\n")));
    	if (pDriverContext == NULL)
    	{
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Memory allocated to pDriverContext\r\n")));
    	}
    	if(pDriverContext->rwBuffer == NULL)
    	{
    		pDriverContext->rwBuffer = (LPWSTR)LocalAlloc(LPTR, BUFFERSIZE * sizeof(WCHAR));
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Memory allocated to rwBuffer\r\n")));
    	}
    	
    	
    	
    	rVal = min(BUFFERSIZE, Count);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): rVal is %d\r\n",rVal)));
    
    	wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, rVal);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %s\r\n",wcsncpy)));
    	
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Error is------- %s %d \r\n",GetLastError(),GetLastError())));
    		//RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %s\r\n",pDriverContext->rwBuffer)));
    	
    		
    
    	RETAILMSG(TRUE,(TEXT("TST_Write(): END\r\n")));
    	
    	return rVal;
    }

    Wednesday, April 2, 2014 10:31 AM
  • Try moving the definition of  Write_Data before you use it that is before you call it.

    Or Give function declaration in beginning of the file for the functions which you are going to use.

    Wednesday, April 2, 2014 11:09 AM
  • Hello

    I am including 

    #include "partap.h"

    and it contains 

    void Create_File();
    void Write_Data(HANDLE);
    void Read_Data(HANDLE);

    Wednesday, April 2, 2014 11:14 AM
  • I think that Misbah was a little harsh, but it was something that you needed to hear - but clearly you don't think that it applied to you.

    This, like some of your other problems, has less to do with Windows CE and more to do with an apparent lack of understanding of some fundamentals of C programming (not that you don't know C programming, but that you are still learning).  This also shows that you fail to do even basic debugging of your application, which would include having one of your coworkers read your code.  

    A quick code review would have shown that you were printing out a function rather than a return value, but heck a review of your debug output would have also turned that up.   It also might have shown that you were incorrectly using a  pointer, which the debug output clearly showed as a data abort.

    With that all said, now that you have changed the code from the original and solved the original 2 programming mistakes, what does the debug output show?  What is the return value now?



    Bruce Eitman (eMVP) Senior Engineer Bruce.Eitman AT Eurotech DOT com My BLOG http://geekswithblogs.net/bruceeitman Eurotech Inc. www.Eurotech.com

    Wednesday, April 2, 2014 12:51 PM
    Moderator
  • Hello Bruce Eitman Sir,

    There are various ways to say the same thing. Eg,

    Get out from this room, I have some work.

    Would you please move out for a while I have some imp. work...

    Acc. to you which one is correct or from eduction point of view not qualification point of view??

    Because, now a days people are qualified but not educated......

    Thursday, April 3, 2014 2:43 AM
  • Hello Bruce Eitman Sir,

    There are various ways to say the same thing. Eg,

    Get out from this room, I have some work.

    Would you please move out for a while I have some imp. work...

    Acc. to you which one is correct or from eduction point of view not qualification point of view??

    Because, now a days people are qualified but not educated......

    What are you saying/asking?  Most of that has no context, or meaning.  Since did not reply to my response, this is even less clear.


    Bruce Eitman (eMVP) Senior Engineer Bruce.Eitman AT Eurotech DOT com My BLOG http://geekswithblogs.net/bruceeitman Eurotech Inc. www.Eurotech.com

    Thursday, April 3, 2014 10:21 AM
    Moderator
  • Hey Misbah,

    we will do one thing... better to stop discussion here, otherwise this thread will flood with uncessory words. So, you deleted all your post from this thread and i do the same thing ...... 

    That will be good for both ....

    Bye and Thanks

    Thursday, April 3, 2014 11:08 AM
  • Allocate memory for pointer before using it.

    pDriverContext in this case.


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com

    Thursday, April 3, 2014 12:57 PM
  • Hello Keshava G N,

    You are right that allocate memory to pointer before using... The memory allocation and de allocation is done in Init and DeInit..

    And for checking this either the memory is allocated to the pointer or not in TST_Write, I am using condition as

    if (pDriverContext == NULL)
    	{
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Memory allocated to pDriverContext\r\n")));
    	}

    and during the time of calling to this write function from app. its not displaying the retailmsg of this condtion which means that the memory is allocated .... 

    Still, I am struggling with this because i want to print the string in TST_Write retailmsg which I am passing from application......

    Thanks to you for your warm reply. 

    :)

    Friday, April 4, 2014 2:46 AM
  • Hello Friends, 

    The problem is self solved... The mistake was here

    rVal = min(BUFFERSIZE, Count);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): rVal is %d\r\n",rVal)));
    
    	wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, rVal);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %s\r\n",wcsncpy)));
    	
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Error is------- %s %d \r\n",GetLastError(),GetLastError())));
    		//RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %s\r\n",pDriverContext->rwBuffer)));
    	

    And this is the correct one code with correct syntax --- 

    wcsncpy((LPWSTR)pDriverContext->rwBuffer, (LPWSTR)pBuffer, Count);
    	RETAILMSG(TRUE,(TEXT("TST_Write(): Count is %d\r\n"),Count));
    	RETAILMSG(TRUE,(TEXT("TST_Write(): WCSNCP is %S\r\n"),pDriverContext->rwBuffer));
    	
    	
    		//RETAILMSG(TRUE,(TEXT("TST_Write(): Error is-------%d \r\n",GetLastError())));
    		RETAILMSG(TRUE,(TEXT("TST_Write(): Written string is------- %S\r\n"),pDriverContext->rwBuffer));

    The error was in RETAILMSG debug syntax and string format specifier .... 
     I am happy that its solved by me with all your support....

    Thanks for warm support ...


    Friday, April 4, 2014 5:09 AM