Hello World (network)

Top  Previous  Next

Hello World (network) sample is similar with Hello World sample but it adds network functionality:

it requests the shared printer name in a dialog as "\\computer name\printer name" (for example if novaPDF Pro is installed on WS1, then you should enter "\\WS1\novaPDF Pro v4")
it automatically registers novapi4.dll if not registered. novapi4.dll must be in the same folder with the HelloWorld (network).exe file.

 

You can share the folder containing HelloWorld (network).exe and novapi4.dll on your network and run the executable from any other computer in the network, with no need to install anything else. It will generate a "HelloWorldNet.pdf" file in the same folder.

 

Source Code snippets (in addition to Hello World source code)

 

{

  //...

  //initialize COM

  hr = CoInitialize(NULL);

  if (FAILED (hr))

  {

    MessageBoxW(NULL, L"Failed to initialize COM", L"novaPDF", MB_OK);

    return hr;

  }

 

  // register the novapi2.dll COM module found in this executable's directory

  hr = RegisterNovaCOM(TRUE);

 

  //create one NovaPdfOptions instance

  INovaPdfOptions *pNova = 0;

  hr = CoCreateInstance(__uuidof(NovaPdfOptions), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions), (LPVOID*) &pNova);

  if (FAILED(hr))  

  {

    MessageBoxW(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK);

    return hr;

  }

  DWORD dwSize = 256;

  WCHAR strWorkStation[256];

 

  //find out computer name

  GetComputerNameW(strWorkStation, &dwSize);

 

  PrinterNameDlg dlg;

  //construct printer name as "\\computer name\printer name"

  dlg.m_strPrinterName.Format(L"\\\\%s\\%s", strWorkStation, PRINTER_NAME);

 

  //get printer name from user

  if (IDCANCEL == dlg.DoModal())

  {

    pNova->Release();

    CoUninitialize();

    return hr;

  }

  CString strPrinterName = dlg.m_strPrinterName;

 

  //initialize NovaPdfOptions and pass printer name

  // if you have an application license for novaPDF SDK,

  // pass both the registration name and the license key to the Initialize() function

  // hr = pNova->Initialize((LPCWSTR)strPrinterName, L"<registration name>", L"<license key>");

  hr = pNova->Initialize((LPCWSTR)strPrinterName, L"", L"", L"");

  //...

}

 

//Register novaPDF COM from novapi2.dll

//novapi2.dll should be in the same folder with the application

HRESULT RegisterNovaCOM(BOOL bRegister = TRUE)

{

  HRESULT hr = E_FAIL;

  WCHAR szFileName[_MAX_PATH] = L"";

 

  //find out application path and name

  DWORD dwRes = GetModuleFileNameW(NULL, szFileName, _MAX_PATH);

 

  if (dwRes > 0 && dwRes < _MAX_PATH)

  {

    WCHAR szDir[_MAX_PATH], szDrive[_MAX_DRIVE];

 

    //get application path

    _wsplitpath(szFileName, szDrive, szDir, NULL, NULL);

    wcscpy(szFileName, szDrive);

    wcscat(szFileName, szDir);

  }

 

  //add COM dll name

  wcscat(szFileName, L"novapi5.dll");

 

  //load COM dll

  HMODULE hNova = LoadLibraryW(szFileName);

 

  typedef HRESULT (STDAPICALLTYPE *DllRegisterServerFunction)(void);

  DllRegisterServerFunction fun = NULL;

 

  if (hNova)

  {

    if (bRegister)

    {

      //get the address of DllRegisterServer function

      fun = (DllRegisterServerFunction) GetProcAddress(hNova, "DllRegisterServer");

    }

    else

    {

      //get the address of DllUnregisterServer function

      fun = (DllRegisterServerFunction) GetProcAddress(hNova, "DllUnregisterServer");

    }

    if (fun)

    {

      // call DllRegisterServer (or DllUnregisterServer)

      hr = fun();

    }

  }

  return hr;

}