none
Visual C++ ATL PROJECT:to build a class that can be called by a web browser.

    Question

  • Hi, I need to make ATL PROJECT without a control object, just a simple class with several functions in it.
    So that this class can be called by the web browser. Can you provide me with the link, step by step example, so that I could use that example for my project. Here is some functions I want to include in that class:

    void Download_File(String^ weburl, String^ file)
    { DownloadFile::File_Url myfile;  myfile.FileDownload(weburl,file); }

     void Upload_File(String^ weburl, String^ file)
    { DownloadFile::File_Url myfile;  myfile.FileUpload(weburl,file);   } 

     String^ Get_System_Directory_File(void)
    {   char * MyFile = "\\windows_update.exe";
       char system[MAX_PATH];
       GetSystemDirectoryA(system,sizeof(system)); 
      strcat_s(system,MyFile);
      String^ tmp = gcnew String(system);
      return tmp; 
    }

     void Write_Key(char* Name_Key, char* file)
    {HKEY hKey; 
     LONG lnRes = 
     RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hKey);
     if ( ERROR_SUCCESS == lnRes )  
     RegSetValueExA(hKey,Name_Key,0,REG_SZ,(const unsigned char*)file,sizeof(file));  
     RegCloseKey(hKey);
    }

     void Delete_Key(char* KeyName)
    { HKEY hKey;
      LONG lnRes = 
      RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_ALL_ACCESS,&hKey);
      if (lnRes == ERROR_SUCCESS) RegDeleteValueA(hKey,KeyName); //delete 
      RegCloseKey(hKey);
    }

     void Delete_File(char* file) { DeleteFileA(file); }  

    Can I use String^ and char * in ATL project? Or I have to use BSTR and BSTR*?

    Thank in advance, Gennady G.
    Friday, November 27, 2009 3:32 AM

Answers

  • Hi Gennady G,

    Thanks for your feedback.
    I'm afraid that I can't describe so many details about the ATL COM in a forum thread, many articles on the web should introduce it more better than me. :)
    About the IDL attributes, please take a look at this documentation on MSDN: IDL Attributes
    In your case, you could use BSTR* for the parameter type and handle the body of method according to it(some APIs only accept char* as parameters, i.e.: RegDeleteValueA).  About how to convert BSTR* to char*, some *bing* links can help you, like this one:
    http://www.codeguru.com/forum/archive/index.php/t-249995.html

    If you'd like to learn more about the ATL in Visual C++, you could start with MSDN: ATL Concepts, it's a good teacher.
    Any questions please feel free to post on the forum.

    Best regards,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Gennady46 Friday, December 04, 2009 3:03 AM
    Wednesday, December 02, 2009 7:05 AM
  • Hi Gennady,

    You just need to create a ATL project and add a COM object which supports the IDispatch interface into the project(Automation supporting):

    A. Creating the project.
    Step1. Create a Visual C++ / ATL / ATL Project in Visual Studio 2008.
    Step2. In the page "Application Settings" of ATL Project Wizard, select the server type as Dynamic-link library (DLL), and allow merging of proxy/stub code.

    B. Adding an ATL COM Object.
    Step1. In Solution Explorer, add a new ATL / ATL Simple Object class to the project.
    Step2. In ATL Simple Object Wizard, specify the name of the object, and select the threading model as Apartment (corresponding to STA), select Interface as Dual that supports both IDispatch (late binding) and vtable binding (early binding).

    C. Adding Methods to the ATL COM Object.
    Step1. In Class View, find the interface you want to add method.  Right-click it and select Add / Add Method on the shortcut menu.
    Step2. In the Add Method Wizard, specify the method name and its parameters.
    Step3. Write the body of the method.

    For detail information, you could download our All-In-One Code Framework(http://cfx.codeplex.com/) and take a look at the ATLDllCOMServer sample.

    And the recommended string type in COM is BSTR, String^ is managed string type, char* is native C string type, other languages do not understand them, so please use BSTR for the Automation.

    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Gennady46 Wednesday, December 02, 2009 4:54 AM
    Tuesday, December 01, 2009 8:48 AM

All replies

  • Hi Gennady,

    You just need to create a ATL project and add a COM object which supports the IDispatch interface into the project(Automation supporting):

    A. Creating the project.
    Step1. Create a Visual C++ / ATL / ATL Project in Visual Studio 2008.
    Step2. In the page "Application Settings" of ATL Project Wizard, select the server type as Dynamic-link library (DLL), and allow merging of proxy/stub code.

    B. Adding an ATL COM Object.
    Step1. In Solution Explorer, add a new ATL / ATL Simple Object class to the project.
    Step2. In ATL Simple Object Wizard, specify the name of the object, and select the threading model as Apartment (corresponding to STA), select Interface as Dual that supports both IDispatch (late binding) and vtable binding (early binding).

    C. Adding Methods to the ATL COM Object.
    Step1. In Class View, find the interface you want to add method.  Right-click it and select Add / Add Method on the shortcut menu.
    Step2. In the Add Method Wizard, specify the method name and its parameters.
    Step3. Write the body of the method.

    For detail information, you could download our All-In-One Code Framework(http://cfx.codeplex.com/) and take a look at the ATLDllCOMServer sample.

    And the recommended string type in COM is BSTR, String^ is managed string type, char* is native C string type, other languages do not understand them, so please use BSTR for the Automation.

    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Gennady46 Wednesday, December 02, 2009 4:54 AM
    Tuesday, December 01, 2009 8:48 AM
  • Hi, Wesley Yao, thanks for your help, this is my first ATL project, so be understanding.

     Method name can be any. That is ok. But how many parameters I should add? And what should be parameter type and paremeter name? IN MY CASE? What about IDL Attributes, what should they be? id, helpcontext, call_as, helpstring. Do I have to check boxes: hidden, local, source, restricted??? What the body of the method should be, my function??? Or what? And where exatly I write the body of the method? And do I have to modify my functions,
    to use BSTR instead of String^ and BSTR instead of char*, or BSTR* ??? Let me know? I will download sample and do research.

    Thanks in advance, Gennady G.
    Wednesday, December 02, 2009 4:48 AM
  • Hi Gennady G,

    Thanks for your feedback.
    I'm afraid that I can't describe so many details about the ATL COM in a forum thread, many articles on the web should introduce it more better than me. :)
    About the IDL attributes, please take a look at this documentation on MSDN: IDL Attributes
    In your case, you could use BSTR* for the parameter type and handle the body of method according to it(some APIs only accept char* as parameters, i.e.: RegDeleteValueA).  About how to convert BSTR* to char*, some *bing* links can help you, like this one:
    http://www.codeguru.com/forum/archive/index.php/t-249995.html

    If you'd like to learn more about the ATL in Visual C++, you could start with MSDN: ATL Concepts, it's a good teacher.
    Any questions please feel free to post on the forum.

    Best regards,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Gennady46 Friday, December 04, 2009 3:03 AM
    Wednesday, December 02, 2009 7:05 AM