0

I'm trying to use the StartXpsPrintJob1 API in C#.

However, I couldn't find any information about it using Google. This method is not listed on pinvoke.net.

Seems I have to translate the C++ code, including classes and interfaces, into C# and use some kind of dllimport? Unfortunately, I have zero knowledge about C++.

How can I acheive this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
nathan1658
  • 175
  • 1
  • 14
  • 2
    Yes, quite unlikely you'll find any sample. This api is wrapped [by the .NET Framework](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-programmatically-print-xps-files). – Hans Passant Aug 12 '18 at 08:30
  • There is a XpsPrint.dll that you can reference it to your project ..[also check](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374565(v=vs.85).aspx) – IteratioN7T Aug 12 '18 at 08:37
  • Thanks for the quick reply, I tried to add reference of `XpsPrint.dll` but it said invalid assembly, then i try to use `TlbImp` to wrap it, still no luck, it said `XpsPrint.dll is not a valid type library` – nathan1658 Aug 12 '18 at 08:40
  • @HansPassant Thanks i'll give it a try, just curious about the efficient improvement of `StartXpsPrintJob1` between `StartXpsPrintJob` – nathan1658 Aug 12 '18 at 08:47
  • 2
    If you are curious about an obscure detail of the XPS printing api then it is best to ask a question about it. – Hans Passant Aug 12 '18 at 08:54
  • Use swig tool to generate API from c++ dll code http://www.swig.org – M.Hassan Aug 12 '18 at 11:34

2 Answers2

1

The error "invalid assembly" is would probably occur if you tried to import XpsPrint.dll into your project as a .Net library: it's actually a "native code" library :(

In theory, you should be able to copy/paste this example into a new MSVS/C# project:

You'll notice that it just "prints" - it doesn't use any XPS-specific APIs.

If that doesn't work for you, this link might also help:

Finally, please note in the Microsoft documentation:

https://msdn.microsoft.com/en-us/library/windows/desktop/ff686814(v=vs.85).aspx

[The XPS Print API is not supported and may be altered or unavailable in the future. Client applications should use the Print Document Package API instead.]

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Seems I have to translate the C++ code, including classes and interfaces, into C# and use some kind of dllimport?

You can save time and effort and use a tool named SWIG, (Sourceforge project page) to generate c# code that generate dllimport.

How to use:

  • Download SWig for windows

  • Create c++ header file that represent StartXpsPrintJob1 , name the file xpsheader.h

  • create interface file e.g example.i

    %module example
     %{
     /* Includes the header in the wrapper code */
     #include "xpsheader.h"
     %}
    
     /* Parse the header file to generate wrappers */
     %include "xpsheader.h"
    
  • Run the command:

    swig -csharp example.i

The tool generate punch of files with one file named examplePINVOKE.cs

In fact, it is important to know that SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the c# target language---exposing the underlying functionality in a very natural manner.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
M.Hassan
  • 10,282
  • 5
  • 65
  • 84