Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:24:05+00:00 2026-05-18T06:24:05+00:00

I have a problem retrieving the information of printer ports. I already use XcvData

  • 0

I have a problem retrieving the information of printer ports. I already use XcvData function to add, configure and delete ports, but I can not get the following to run as I expect it to:

public PrinterNativeMethods.PORT_DATA_1 GetPortData1FromPort(string serverName, string portName)
{
    IntPtr printerHandle;
    PrinterNativeMethods.PRINTER_DEFAULTS defaults = new PrinterNativeMethods.PRINTER_DEFAULTS
    {
        DesiredAccess = PrinterNativeMethods.PrinterAccess.ServerAdmin
    };

    string connection = string.Format(@"{0},XcvPort {1}", serverName, portName);

    PrinterNativeMethods.OpenPrinter(connection, out printerHandle, ref defaults);

    PrinterNativeMethods.CONFIG_INFO_DATA_1 configData = new PrinterNativeMethods.CONFIG_INFO_DATA_1
    {
        dwVersion = 1,
    };

    uint size = (uint)Marshal.SizeOf(configData);

    IntPtr pointer = Marshal.AllocHGlobal((int)size);
    Marshal.StructureToPtr(configData, pointer, true);

    PrinterNativeMethods.PORT_DATA_1 portData = new PrinterNativeMethods.PORT_DATA_1();
    uint portDataSize = (uint)Marshal.SizeOf(portData);
    IntPtr portDataHandle = Marshal.AllocHGlobal((int)portDataSize);

    try
    {
        uint outputNeeded;
        uint status;

        var retVal = PrinterNativeMethods.XcvData(printerHandle, "GetConfigInfo", pointer, size, out portDataHandle, portDataSize, out outputNeeded, out status);
        //portDataHandle now points to a different location!? Unmarshalling will fail:
        portData = (PrinterNativeMethods.PORT_DATA_1)Marshal.PtrToStructure(portDataHandle, typeof(PrinterNativeMethods.PORT_DATA_1));

    }
    finally
    {
        PrinterNativeMethods.ClosePrinter(printerHandle);
        Marshal.FreeHGlobal(pointer);
        Marshal.FreeHGlobal(portDataHandle);
    }

    return portData;
}

from PrinterNativeMethods:

    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern int XcvData(
        IntPtr handle,
        string dataName,
        IntPtr inputData,
        uint inputDataSize,
        out IntPtr outputData,
        uint outputDataSize,
        out uint outputNeededSize,
        out uint status);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct PORT_DATA_1
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
        public string sztPortName;

        public uint dwVersion;

        public uint dwProtocol;

        public uint cbSize;

        public uint dwReserved;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 49)]
        public string sztHostAddress;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33h)]
        public string sztSNMPCommunity;

        public uint dwDoubleSpool;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
        public string sztQueue;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string sztIPAddress;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 540)]
        public byte[] Reserved;

        public uint dwPortNumber;

        public uint dwSNMPEnabled;

        public uint dwSNMPDevIndex;
    }

Additional comment: I cannot use WMI or prnadmin.dll as an alternative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:24:05+00:00Added an answer on May 18, 2026 at 6:24 am

    The problem you are having is in the definition of your XcvData definition. The outputData parameter by MS’s definition is simply wanting a pointer to write data to, an IntPtr is a pointer, however by setting the parameter to out IntPtr you are making it a pointer to a pointer, this is why the address of your parameter appears to be changing. Changing the signature to the below will fix your problem.

    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern int XcvData(
        IntPtr handle,
        string dataName,
        IntPtr inputData,
        uint inputDataSize,
        IntPtr outputData,
        uint outputDataSize,
        out uint outputNeededSize,
        out uint status);
    

    You can also avoid some unnecessary allocation/deallocation by changing things around a bit.

    Change your method signature for XcvData to

    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern int XcvData(
        IntPtr handle,
        string dataName,
        IntPtr inputData,
        uint inputDataSize,
        ref PORT_DATA_1 outputData,
        uint outputDataSize,
        out uint outputNeededSize,
        out uint status);
    

    Assuming that you will be using XcvData for more than just this single call you can make multiple references to it with slightly different signatures by setting the EntryPoint property on the DllImport Attribute.

    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint="XcvData")]
    internal static extern int XcvDataGetPortData1(
        IntPtr handle,
        string dataName,
        IntPtr inputData,
        uint inputDataSize,
        ref PORT_DATA_1 outputData,
        uint outputDataSize,
        out uint outputNeededSize,
        out uint status);
    

    I have given this a quick test on my machine and can confirm that this will fix your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with retrieving information from a XML tree. My XML has
For retrieving the accounts (information) in Android versions since 2.0 you can use the
So I managed to solve a problem of retrieving information from the bundle, but
I have a problem when retrieving information from an imported attribute. The attribute remains
We have a problem with the Euro character when saving and retrieving it from
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with fancybox. I want to write a function that will run
heres the problem, i have a database thats hold information on marker images: i'm
I have a problem retrieving default values from the settings bundle when first launching
I have a problem retrieving the exact css property value (in '%') on firefox.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.