CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2007
    Posts
    2

    Creating TCP/IP Port programatically

    Hi, I am trying to create a TCP/IP port programatically in win32.

    Here is my code.


    HANDLE hXcv;PRINTER_DEFAULTS pd = {NULL, NULL, SERVER_ACCESS_ADMINISTER};TCHAR szBuf[MAX_PATH] ={0};TCHAR szMsg[512];DWORD dwLastError;::lstrcpy(szBuf, TEXT(",XcvMonitor Standard TCP/IP Port"));if(OpenPrinter(szBuf, &hXcv, &pd) != FALSE ){ OutputDebugString(TEXT("Hello. It worked....!"));}else{ dwLastError = GetLastError(); wsprintf(szMsg, TEXT("OpenPrinter return error:%d\n"), dwLastError); OutputDebugString(szMsg);}> But the problem is OpenPrinter API failing and returning
    error code as 5 (ERROR_ACCESS_DENIED).

    > But when I try to create the port manually (print properties), port is created.
    > I have logged in as an administrator and I have all permissions.

    I am working on Windows Server 2008 R2 Statndard (64 bit OS)

    Note: OpenPrinter API working fine if I turn-OFF User Account Settings (UAC). Failing if UAC is ON

    Please tell me what's wrong with my code.

    Thank you in advance.


    Regards
    msr

  2. #2
    Join Date
    Oct 2010
    Posts
    68

    Re: Creating TCP/IP Port programatically

    I have no idea what that code says but try right clicking your app and selecting 'Run as Administrator'.

  3. #3
    Join Date
    Jun 2007
    Posts
    2

    Re: Creating TCP/IP Port programatically

    Hi this is the code....


    HANDLE hXcv;

    PRINTER_DEFAULTS pd = {NULL, NULL, SERVER_ACCESS_ADMINISTER};

    TCHAR szBuf[MAX_PATH] ={0};

    TCHAR szMsg[512];

    DWORD dwLastError;

    ::lstrcpy(szBuf, TEXT(",XcvMonitor Standard TCP/IP Port"));

    if(OpenPrinter(szBuf, &hXcv, &pd) != FALSE )
    {
    OutputDebugString(TEXT("Hello. It worked....!"));
    }
    else
    {
    dwLastError = GetLastError();
    wsprintf(szMsg, TEXT("OpenPrinter return error:%d\n"), dwLastError);
    OutputDebugString(szMsg);
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating TCP/IP Port programatically

    According to MSDN article OpenPrinter Function:
    If a user does not have permission to open a specified printer or print server with the desired access, the OpenPrinter call will fail with a return value of zero and GetLastError will return the value ERROR_ACCESS_DENIED.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured