Thursday, April 24, 2014

How to Delete a Directory without looking its contents using SHFileOperation

BOOL MyFileOperations :: DeleteImageDirectory(Cstring strDirectory)
{  
     Cstring strDirectoryName;
      SHFILEOPSTRUCT shFile;
      int nResult = 0;
      strDirectoryName = strDirectory;
      ZeroMemory (&shFile, sizeof(SHFILEOPSTRUCT));

      shFile.wFunc     = FO_DELETE;
      strDirectoryName = strDirectoryName +_T('\0\0');
      shFile.pFrom     = strDirectoryName.c_str();
      shFile.pTo       = NULL;
      shFile.fFlags    = FOF_SILENT + FOF_NOCONFIRMATION;
      nResult          = SHFileOperation(&shFile);
      if(nResult != 0)
      {              
           MsgBox("Error");
           return FALSE;
      }
      else
      {
           MsgBox("Sucess");    
      }  
      return TRUE;

}

Monday, April 21, 2014

A simple Technique to Convert CString to Char or char[ ] array

char mychar;

CString myCString,myLetter;

myCString=_T("From this I want 1st Character");

myLetter=myCString.Mid(0,1);//Now myLetter="F"

 TCHAR* pmyLetter;
 
pmyLetter=myLetter.GetBuffer();

myLetter.ReleaseBuffer();

 TCHAR pszmyLetter[1];
     
_stprintf(pszmyLetter,_T("%s"),pmyLetter);
               
mychar=pszmyLetter[0];//Now sucessfully mychar='F'


Like wise you can do for character array char[] also from CString I know this is not professional but going through all conversions i failed to acheieve sucessful conversion in Visual Studio 2013 so this method works for me fine with out any debug errors or assertion warnings.

Hope this will help you :-)

Friday, April 18, 2014

ATL Service PreMessageLoop is not Called or ATL Service is cannot be Registered


I had a problem as I mentioned above and I solved in a way as follows

Step 1:-

          Check your Service APPID is registered in HKEY_CLASSES_ROOT

             In this See AppID folder by Expanding it you can see AppID of different Application

         See whether your AppID of ATL service is there

          Your ATL Service AppID is given in the <class>ServiceModule  .cpp file

           It is mentioned along with DECLARE_REGISTRY_APPID_RESOURCEID

         It will look like some thing like this

          DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MYService,"{6F201537-FA02-47DA-B778-A7D5E27CEDF2}")

          this is your AppID for Application see this is registed in Registry

  Run->regedit->HKEY_CLASSES_ROOT->AppID find 6F201537-FA02-47DA-B778-A7D5E27CEDF2

          if this doesnot exists that means your application is not registered and the service fails to start up

Step 2:- To Register service and Make it Startup

            Right Click Visual Studio Icon->Run As Administrator

            Open your ATL project want to register

             Project->Linker->Per-user Redirection set to No (If Project is 2010 and above else Yes)
     
             Project->Linker->Register Output Set to Yes

Now Re-build apllication and run see the ATL service is registed and works fine

Remove (_CRT_SECURE_NO_WARNINGS) in Visual Studio

Not automatically, no. You can create a project template as BlueWandered suggested or create a custom property sheet that you can use for your current and all future projects.

Open up the Property Manager (View->Property Manager)
In the Property Manager Right click on your project and select "Add New Project Property Sheet"
Give it a name and create it in a common directory. The property sheet will be added to all build targets.
Right click on the new property sheet and select "Properties". This will open up the properties and allow you to change the settings just like you would if you were editing them for a project.
Go into "Common Properties->C/C++->Preprocessor"
Edit the setting "Preprocessor Definitions" and add _CRT_SECURE_NO_WARNINGS.
Save and you're done.
Now any time you create a new project, add this property sheet like so...

Open up the Property Manager (View->Property Manager)
In the Property Manager Right click on your project and select "Add Existing Project Property Sheet"
The benefit here is that not only do you get a single place to manage common settings but anytime you change the settings they get propagated to ALL projects that use it. This is handy if you have a lot of settings like _CRT_SECURE_NO_WARNINGS or libraries like Boost that you want to use in your projects.

How Convert LPCSTR to LPCTSTR in VC++ (Nothing works)

If Nothing works for you Try this I succeeded

Create a Dummy Helper Structure

typedef struct _tag_myconvhelper
{
    CString myhelper;
}MYCONVHELPER;

In your Program don't forget to include tchar.h and CString

create an object for _tag_myconvhelper

MYCONVHELPER objmyconhelper;

LPCSTR lpszfromstring =_T("This is the String");

objmyconhelper.myhelper = lpszfromstring ;

LPCTSTR lpsztostring = objmyconhelper.myhelper;

Windows automatically converts it for you no need of complex conversion method :-)

How to Covert a Simple CString to LPCSTR in VC++

CString Str1("This is my CString");

CString Str1Ref(Str1);//Ref CString

CStringA StrA(Str1Ref);//a helper String

LPCSTR lpszmyString =StrA; // Now lpszmyString  contains This is my CString


Error #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version

This Occurs when you Use MFC dependcy in VC++ other than MFC Application

How to Remove This Error:

In Visual Studio

Goto Project->Properties->Configuration Properties->General

You can see a Row Named Use of MFC from that Select Use MFC in a Shared DLL from Combo Box
then click Apply and Ok the error is removed after a Build :)

VC++ shows error VCEnd exited with code 2147024891

Starting from Windows Vista, including Vista, Win2008 Server, Windows7, build process cannot write to HKEY_CLASSES_ROOT (HKCR) hives unless the process is started as administrator. To be able to register in non-administrator mode, VS2008 and VS2010 offer the feature of "registrer per user". The registration information is written to HKEY_CURRENT_USER (HKCU).

You can set Linker -> General -> Per User ReDirections to "true" if Linker -> General -> Register Output is set to "true if you use the built registration step of the project system.

If you have your own custom registration step, you can use the per user registration command: regsvr32 /s /n /i:user "xxxx.dll".

If VS2010 is installed on Windows XP, you don't need to have the elevated privilege or per user registration to register your application since the security requirement is OS specific.

ATL Project in VS2010 RC - how should I run VS2010.

RefLink :-http://social.msdn.microsoft.com/Forums/vstudio/en-US/82ed3690-c622-4262-a965-33eb3870dcfc/registering-service-in-vsnet2010-build-event-gets-following-error-regservervcend-exited-with?forum=msbuild

Error LNK2019 when using GetFileVersionInfoSize()

I am getting same error, while upgrading the VS6.0 application to VS2013 platform.
a. error LNK2019: unresolved external symbol _GetFileVersionInfoSizeA@8 referenced in function _main
b. error LNK2019: unresolved external symbol _GetFileVersionInfoA@16 referenced in function _main
c. error LNK2019: unresolved external symbol _VerQueryValueA@16 referenced in function _main

Resolution:
I found that it is due to missing reference to library "Version.lib".
a. For VS6.0 add it to Project Setting->Link->library modules
b. For VS2012 or 2013 add to Project Properties->Linker->Input->Additional Dependancies
Type Version.lib and Click Ok problem Solved :)



Courtesy: Stack Over Flow


Reason:

The functions GetFileVersionInfo and GetFileVersionInfoSize are defined in Version.dll and Version.lib so make sure, you are liking to the appropriate libraries.

Thursday, April 3, 2014

Simple Trick to get Date&Time with Milliseconds (Batch File with Note Pad)

Type the following code in Notepad

@echo off
:start
echo Date:%date% Time:%time%
goto start

Save text file  as <anyname>.bat   

For Eg : time.bat

Double click this batch file

See...what happens