none
Add printer C++ in CLR RRS feed

  • Question

  • Hi Guys

    I'm wondering if some kind soul could direct me to methods in Visual c++ 2010 to:

    add a network printer

    set the default printer

     

    Regards

    Brian


    • Edited by nz556 Sunday, October 2, 2011 1:15 AM
    Sunday, October 2, 2011 1:15 AM

Answers

  • use StringToHGlobalUni  and LPCWSTR instead. It looks like you have a Unicode build.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Sunday, October 9, 2011 2:52 PM
  • You need to link Winspool.lib. This is documented in the documentation of AddPrinterConnection.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Monday, October 10, 2011 11:32 PM
  • You can call AddPrinter/SetDefaultPrinter even when in a managed method.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Tuesday, October 11, 2011 12:03 AM
    Monday, October 3, 2011 2:32 AM
  • Hi

    Well, frustration got the better of me and I've resorted to:

        //Method 4
        String^ printer = "\\\\SERVER\\leap";
        ProcessStartInfo^ pSi = gcnew ProcessStartInfo("rundll32.exe");
        pSi->Arguments = String::Format("printui.dll,PrintUIEntry /in /q /n{0}", printer);
        pSi->WindowStyle = ProcessWindowStyle::Hidden;
        Process::Start(pSi);

    ,

    However, I'm not in a position to test it though.

     

     

    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Monday, October 10, 2011 11:22 PM

All replies

  • You can call AddPrinter/SetDefaultPrinter even when in a managed method.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Tuesday, October 11, 2011 12:03 AM
    Monday, October 3, 2011 2:32 AM
  • Hi

    Followed the advice but I'm struggling with AddPrinter. I've got:

    String^ z = "\\\\SERVER\\leap";
    LPTSTR network_printer = ???;
    AddPrinterConnection(network_printer);

    My question is how do I convert the String^ z to a LPTSTR value which AddPrinter requires?

    Can someone help please?

     

     

    Tuesday, October 4, 2011 7:21 AM
  • http://support.microsoft.com/kb/311259

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, October 4, 2011 3:13 PM
  • Been off-line for a week but now really close thanks to your help. Still learning C++ so maybe a really stupid question.

    I have:

        String^ printer = "\\\\SERVER\\leap";
        LPTSTR hmmm = (LPTSTR)System::Runtime::InteropServices::Marshal::StringToHGlobalAuto(printer).ToInt32();

        AddPrinterConnection(hmmm);

    First two lines compile correctly but the third one doesn't. AddPrinterConnection requires a LPTSTR pname parameter. Can you guide me to this final piece in the jigsaw?

     

    BTW, I'm using C++ 2010.

     



    • Edited by nz556 Sunday, October 9, 2011 10:18 AM
    Sunday, October 9, 2011 10:12 AM
  • use StringToHGlobalUni  and LPCWSTR instead. It looks like you have a Unicode build.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Sunday, October 9, 2011 2:52 PM
  • Hi

    Thanks for the promt reply but no closer.

     

    Yes, I'm using Unicode build.

        String^ printer = "\\\\SERVER\\leap";
        LPCWSTR hmmm = (LPCWSTR)System::Runtime::InteropServices::Marshal::StringToHGlobalUni(printer).ToInt32();
        AddPrinterConnection(hmmm);

    gives:error C2664: 'AddPrinterConnectionW' : cannot convert parameter 1 from 'LPCWSTR' to 'LPWSTR'

    >          Conversion loses qualifiers

    Now, http://msdn.microsoft.com/en-us/library/dd183344%28v=VS.85%29.aspx is the refernce to AddPrinterConnection and the syntax is:

    BOOL AddPrinterConnection(
      __in  LPTSTR pName
    );

    Thoughts most appreciated.

    Sunday, October 9, 2011 10:55 PM
  • AddPrinter((LPWSTR)hmmm);

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, October 10, 2011 12:46 PM
  • No Joy.

    Sorry to bug you but this is driving me crazy.

        //Method 1
        String^ printer = "\\\\SERVER\\leap";
        LPCWSTR hmmm = (LPCWSTR)System::Runtime::InteropServices::Marshal::StringToHGlobalUni(printer).ToInt32();
        AddPrinterConnection((LPWSTR)hmmm);

    produces:

    1>trythis.obj : error LNK2028: unresolved token (0A000092) "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>trythis.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>D:\Visual Studio 2010\Projects\trythis\Debug\trythis.exe : fatal error LNK1120: 2 unresolved externals

        //Method 2
        String^ printer = "\\\\SERVER\\leap";
        LPWSTR str = (LPWSTR)Marshal::StringToHGlobalUni(printer).ToPointer();
        AddPrinterConnection(str);

    produces:

    1>trythis.obj : error LNK2028: unresolved token (0A000092) "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>trythis.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>D:\Visual Studio 2010\Projects\trythis\Debug\trythis.exe : fatal error LNK1120: 2 unresolved externals

     

        //Method 3
        String^ printer = "\\\\SERVER\\leap";
        LPTSTR str = (LPTSTR)Marshal::StringToHGlobalUni(printer).ToPointer();
        AddPrinterConnection(str);

    produces:

    1>trythis.obj : error LNK2028: unresolved token (0A000092) "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>trythis.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall AddPrinterConnectionW(wchar_t *)" (?AddPrinterConnectionW@@$$J14YGHPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
    1>D:\Visual Studio 2010\Projects\trythis\Debug\trythis.exe : fatal error LNK1120: 2 unresolved externals

    Monday, October 10, 2011 7:51 PM
  • Hi

    Well, frustration got the better of me and I've resorted to:

        //Method 4
        String^ printer = "\\\\SERVER\\leap";
        ProcessStartInfo^ pSi = gcnew ProcessStartInfo("rundll32.exe");
        pSi->Arguments = String::Format("printui.dll,PrintUIEntry /in /q /n{0}", printer);
        pSi->WindowStyle = ProcessWindowStyle::Hidden;
        Process::Start(pSi);

    ,

    However, I'm not in a position to test it though.

     

     

    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Monday, October 10, 2011 11:22 PM
  • You need to link Winspool.lib. This is documented in the documentation of AddPrinterConnection.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by nz556 Monday, October 10, 2011 11:42 PM
    Monday, October 10, 2011 11:32 PM
  • Magic. Works fine.

    Not much better than a novice at C++ but ever so eager to get better.

     

    Appreciate your kind and generous use of your time.

     

     

    Monday, October 10, 2011 11:42 PM