BOOL WINAPI FaxSendDocument(
  HANDLE FaxHandle,          // handle to the fax server
  LPCTSTR FileName,          // file with data to transmit
  PFAX_JOB_PARAM JobParams,  // pointer to job information structure
  CONST FAX_COVERPAGE_INFO *CoverpageInfo OPTIONAL, 
                             // pointer to local cover page structure
  LPDWORD FaxJobId           // fax job identifier
);因为*CoverpageInfo可以为null,所有想转成IntPtr类型,请问怎么转为IntPtr类型?

解决方案 »

  1.   

    [DllImport("winfax.dll", SetLastError = true)]
    static extern bool FaxSendDocument (IntPtr FaxHandle, string FileName,
       ref FAX_JOB_PARAM JobParams, ref FAX_COVERPAGE_INFO CoverpageInfo,
       ref uint FaxJobId);//If you use FAX_JOB_PARAM and FAX_COVERPAGE_INFO as a CLASS !
    [DllImport("winfax.dll", SetLastError = true)]
    [DllImport(DLLWinFax, SetLastError = true)]
    static extern bool FaxSendDocument(IntPtr FaxHandle, string FileName,
    FAX_JOB_PARAM JobParams, FAX_COVERPAGE_INFO CoverpageInfo,
      out uint FaxJobId);
      

  2.   

    谢谢楼上的,我现在是这样的
    [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxSendDocument(int FaxHandle, string FileName, 
    [In] ref FAX_JOB_PARAM JobParams, [In] ref FAX_COVERPAGE_INFO CoverpageInfo,
    [Out] out int FaxJobId);
    但是platform文档里说
    CoverpageInfo 
    [in] Pointer to a FAX_COVERPAGE_INFO structure that contains personal data to display on the cover page of the fax document. This parameter must be NULL if a cover page is not required. 所以我想穿一个null给他,但是老是报参数不对。系统错误编号为87(The parameter is incorrect.)
    IntPtr coverPageNull = ((IntPtr)((int)(0)));
    if(!FaxSendDocument(hFax, faxDocument, ref jobParam, ref coverPageNull, out faxJobID))
      

  3.   

    int coverPageNull = 0;
    if(!FaxSendDocument(hFax, faxDocument, ref jobParam, ref coverPageNull, out faxJobID))
      

  4.   

    你把FAX_COVERPAGE_INFO的定义方式由struct改为class,然后用null就行了。
      

  5.   

    ref FAX_COVERPAGE_INFO CoverpageInfo
    这个 FAX_COVERPAGE_INFO 类型能不能能null值?
      

  6.   

    我还是把原代码给你们看看,你们帮我调调
    using System;
    using System.Runtime.InteropServices;
    public abstract class Win32
    {
    public static readonly int INFINITE = -1;
    public static readonly int INVALID_HANDLE_VALUE = -1; [DllImport("kernel32")]
    public static extern int CreateIoCompletionPort(int FileHandle, int ExistingCompletionPort, 
    int CompletionKey, int NumberOfConcurrentThreads); [DllImport("kernel32")]
    public static extern int GetQueuedCompletionStatus(int CompletionPort, 
    ref int lpNumberOfBytesTransferred, ref int lpCompletionKey, IntPtr lpOverlapped, 
    int dwMilliseconds); [DllImport("kernel32")]
    public static extern int CloseHandle(int hObject);
    }public abstract class WinFax
    {
    #region member
    // private const IntPtr NullPtr = ((IntPtr)((int)(0)));
    // [CLSCompliant(false)]
    // unsafe IntPtr Null = new IntPtr( void* value ); #endregion
    #region Enumerations public enum FAX_PORT_OPEN_TYPE
    {
    PORT_OPEN_QUERY = 1,
    PORT_OPEN_MODIFY = 2
    } public enum FAX_ENUM_JOB_SEND_ATTRIBUTES
    {
    JSA_NOW = 0, // Send now
    JSA_SPECIFIC_TIME, // Send at specific time
    JSA_DISCOUNT_PERIOD // Send at server configured discount period
    } [FlagsAttribute()]
    public enum WinFaxEvent
    {
    FEI_DIALING = 0x00000001,
    FEI_SENDING = 0x00000002,
    FEI_RECEIVING = 0x00000003,
    FEI_COMPLETED = 0x00000004,
    FEI_BUSY = 0x00000005,
    FEI_NO_ANSWER = 0x00000006,
    FEI_BAD_ADDRESS = 0x00000007,
    FEI_NO_DIAL_TONE = 0x00000008,
    FEI_DISCONNECTED = 0x00000009,
    FEI_FATAL_ERROR = 0x0000000a,
    FEI_NOT_FAX_CALL = 0x0000000b,
    FEI_CALL_DELAYED = 0x0000000c,
    FEI_CALL_BLACKLISTED = 0x0000000d,
    FEI_RINGING = 0x0000000e,
    FEI_ABORTING = 0x0000000f,
    FEI_ROUTING = 0x00000010,
    FEI_MODEM_POWERED_ON = 0x00000011,
    FEI_MODEM_POWERED_OFF = 0x00000012,
    FEI_IDLE = 0x00000013,
    FEI_FAXSVC_ENDED = 0x00000014,
    FEI_ANSWERED = 0x00000015,
    FEI_JOB_QUEUED = 0x00000016,
    FEI_DELETED = 0x00000017,
    FEI_INITIALIZING = 0x00000018,
    FEI_LINE_UNAVAILABLE = 0x00000019,
    FEI_HANDLED = 0x0000001a,
    FEI_FAXSVC_STARTED = 0x0000001b
    }
    #endregion #region Structs [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
    public ushort wYear;
    public ushort wMonth;
    public ushort wDayOfWeek;
    public ushort wDay;
    public ushort wHour;
    public ushort wMinute;
    public ushort wSecond;
    public ushort wMilliseconds;
    } [StructLayout(LayoutKind.Sequential)]
    public struct FAX_DEVICE_STATUS
    {
    public int SizeOfStruct; // size of this structure
    public string CallerId; // caller id string
    public string Csid; // station identifier
    public int CurrentPage; // current page
    public int DeviceId; // permanent line id
    public string DeviceName; // device name
    public string DocumentName; // document name
    public int JobType; // send or receive?
    public string PhoneNumber; // sending phone number
    public string RoutingString; // routing information
    public string SenderName; // sender name
    public string RecipientName; // recipient name
    public int Size; // size in bytes of the document
    public FILETIME StartTime; // starting time of the fax send/receive
    public int Status; // current status of the device, see FPS_??? masks
    public string StatusString; // status string if the Status field is zero. this may be NULL.
    public FILETIME SubmittedTime; // time the document was submitted
    public int TotalPages; // total number of pages in this job
    public string Tsid; // transmitting station identifier
    public string UserName; // user that submitted the active job
    } [StructLayout(LayoutKind.Sequential)]
    public struct FAX_COVERPAGE_INFO
    {
    public int SizeOfStruct; // Size of this structure
    //
    // general
    //
    public string CoverPageName; // coverpage document name
    public bool UseServerCoverPage; // coverpage exists on the fax server
    //
    // Recipient information
    //
    public string RecName; //
    public string RecFaxNumber; //
    public string RecCompany; //
    public string RecStreetAddress; //
    public string RecCity; //
    public string RecState; //
    public string RecZip; //
    public string RecCountry; //
    public string RecTitle; //
    public string RecDepartment; //
    public string RecOfficeLocation; //
    public string RecHomePhone; //
    public string RecOfficePhone; //
    //
    // Sender information
    //
    public string SdrName; //
    public string SdrFaxNumber; //
    public string SdrCompany; //
    public string SdrAddress; //
    public string SdrTitle; //
    public string SdrDepartment; //
    public string SdrOfficeLocation; //
    public string SdrHomePhone; //
    public string SdrOfficePhone; //
    //
    // Misc information
    //
    public string Note; // public string Subject; //
    public SYSTEMTIME TimeSent; // Time the fax was sent
    public int PageCount; // Number of pages
    } [StructLayout(LayoutKind.Sequential)]
    public struct FAX_EVENT
    {
    public int SizeOfStruct; // Size of this structure
    public FILETIME TimeStamp; // Timestamp for when the event was generated
    public int DeviceId; // Permanent line id
    public int EventId; // Current event id
    public int JobId; // Fax Job Id, 0xffffffff indicates inactive job
    } [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    public struct FAX_JOB_PARAM
    {
    public int SizeOfStruct; // size of this structure
    public string RecipientNumber; // recipient fax number
    public string RecipientName; // recipient name
    public string Tsid; // transmitter's id
    public string SenderName; // sender name
    public string SenderCompany; // sender company
    public string SenderDept; // sender department
    public string BillingCode; // billing code
    public int ScheduleAction; // when to schedule the fax, see JSA defines
    public SYSTEMTIME ScheduleTime; // time to send the fax when JSA_SPECIFIC_TIME is used (must be local time)
    public int DeliveryReportType; // delivery report type, see DRT defines
    public string DeliveryReportAddress; // email address for delivery report (ndr or dr) thru MAPI / SMTP
    public string DocumentName; // document name (optional)
    public IntPtr CallHandle; // optional call handle //[MarshalAs(UnmanagedType., SizeConst=3)]
    public IntPtr[] Reserved; // reserved for ms use only
    }
    #endregion #region Externs [DllImport("winfax.dll", SetLastError=true)]
    public static extern int FaxConnectFaxServer(string MachineName,
    [Out] out int FaxHandle); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxInitializeEventQueue(int FaxHandle, int
    CompletionPort, int CompletionKey, int hWnd, int MessageStart); [DllImport("winfax.dll", SetLastError=true)]
    public static extern void FaxFreeBuffer(IntPtr Buffer); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxClose(int FaxHandle); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxCompleteJobParams([In, Out] ref
    FAX_JOB_PARAM JobParams, [In, Out] ref FAX_COVERPAGE_INFO CoverpageInfo); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxSendDocument(int FaxHandle, string FileName, 
    [In] ref FAX_JOB_PARAM JobParams, [In] ref FAX_COVERPAGE_INFO CoverpageInfo,
    [Out] out int FaxJobId); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxSendDocument(int FaxHandle, string FileName, 
    [In] ref FAX_JOB_PARAM JobParams, [In] ref IntPtr CoverpageInfo, 
    [Out] out int FaxJobId); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxOpenPort(int FaxHandle, int DeviceId, int Flags, 
    [Out] int FaxPortHandle); [DllImport("winfax.dll", SetLastError=true)]
    public static extern bool FaxGetDeviceStatus(int FaxPortHandle,
    [Out] out FAX_DEVICE_STATUS DeviceStatus); #endregion
      

  7.   

    public static bool SendFax(string faxDocument, string faxNumber)
    {
    bool bTerminate = false;
    int hCompletionPort = 0;
    int completionKey = 0;
    int bytesSent = 0;
    int hFax = 0;
    //IntPtr hFax = new IntPtr(0);
    //IntPtr hPort = new IntPtr(0);
    int hPort = 0;
    int faxJobID = 0;
    FAX_DEVICE_STATUS deviceStatus;
    FAX_EVENT faxEvent = new FAX_EVENT();
    FAX_JOB_PARAM jobParam = new FAX_JOB_PARAM();
    FAX_COVERPAGE_INFO coverPageInfo = new FAX_COVERPAGE_INFO(); faxEvent.SizeOfStruct = Marshal.SizeOf(typeof(FAX_EVENT));
    IntPtr pFaxEvent = Marshal.AllocHGlobal(faxEvent.SizeOfStruct);
    Marshal.StructureToPtr(faxEvent, pFaxEvent, true); jobParam.SizeOfStruct = Marshal.SizeOf(typeof(FAX_JOB_PARAM));
    IntPtr pJobParam = Marshal.AllocHGlobal(jobParam.SizeOfStruct);
    Marshal.StructureToPtr(jobParam, pJobParam, true); coverPageInfo.SizeOfStruct = Marshal.SizeOf(typeof(FAX_COVERPAGE_INFO));
    IntPtr pCoverPageInfo = Marshal.AllocHGlobal(coverPageInfo.SizeOfStruct);
    Marshal.StructureToPtr(coverPageInfo, pCoverPageInfo, true); // connect to fax service
    if(FaxConnectFaxServer(null, out hFax) == 0)
    {
    Console.WriteLine("GetLastWin32Error: " + Marshal.GetLastWin32Error().ToString()); //_tprintf( TEXT("FaxConnectFaxServer failed, ec = %d\n"),GetLastError() );
    return false;
    } // create completion port to poll against while waiting for fax completion
    hCompletionPort = Win32.CreateIoCompletionPort(Win32.INVALID_HANDLE_VALUE, 0, 0, 0); if(hCompletionPort == 0)
    {
    Console.WriteLine("GetLastWin32Error: " + Marshal.GetLastWin32Error().ToString()); //_tprintf( TEXT("CreateIoCompletionPort failed, ec = %d\n"),GetLastError());
    FaxClose(hFax);
    return false;
    } if (!FaxInitializeEventQueue( hFax, hCompletionPort, 0, 0, 0))
    {
    Console.WriteLine("GetLastWin32Error: " + Marshal.GetLastWin32Error().ToString()); //_tprintf( TEXT("FaxInitializeEventQueue failed, ec = %d\n"),GetLastError() );
    FaxClose(hFax);
    return false;
    } FaxCompleteJobParams(ref jobParam, ref coverPageInfo); jobParam.RecipientNumber = faxNumber;
    jobParam.ScheduleAction = (int)FAX_ENUM_JOB_SEND_ATTRIBUTES.JSA_NOW;
    jobParam.CallHandle = new IntPtr(0);// IntPtr coverPageNull = new IntPtr(0);
    IntPtr coverPageNull = ((IntPtr)((int)(0)));
    if(!FaxSendDocument(hFax, faxDocument, ref jobParam, ref coverPageNull, out faxJobID))
    {
    Console.WriteLine("GetLastWin32Error: " + Marshal.GetLastWin32Error().ToString()); //jobParam = (FAX_JOB_PARAM) Marshal.PtrToStructure(pJobParam,typeof(FAX_JOB_PARAM));
    //coverPageInfo = (FAX_COVERPAGE_INFO) Marshal.PtrToStructure(pCoverPageInfo, typeof(FAX_COVERPAGE_INFO)); //_tprintf( TEXT("FaxSendDocument failed, ec = %d \n"),GetLastError() ); FaxClose(hFax);
    Win32.CloseHandle(hCompletionPort);
    FaxFreeBuffer(pJobParam);
    FaxFreeBuffer(pCoverPageInfo);
    return false;
    } //_tprintf(TEXT("Queued document %s for transmition to %s, JobID = %d\n"), Document, Number, JobId); FaxFreeBuffer(pJobParam);
    FaxFreeBuffer(pCoverPageInfo); Marshal.StructureToPtr(faxEvent, pFaxEvent, true); while (!bTerminate &&
    (Win32.GetQueuedCompletionStatus(
    hCompletionPort,
    ref bytesSent,
    ref completionKey,
    pFaxEvent,
    Win32.INFINITE) != 0))
    { faxEvent = (FAX_EVENT) Marshal.PtrToStructure(pFaxEvent,
    typeof(FAX_EVENT)); //_tprintf( TEXT("Received event 0x%x\n"),FaxEvent->EventId); switch ((WinFaxEvent) faxEvent.EventId)
    {
    case WinFaxEvent.FEI_IDLE:
    case WinFaxEvent.FEI_COMPLETED:
    case WinFaxEvent.FEI_MODEM_POWERED_ON:
    case WinFaxEvent.FEI_MODEM_POWERED_OFF:
    case WinFaxEvent.FEI_FAXSVC_ENDED:
    bTerminate = true;
    break; case WinFaxEvent.FEI_JOB_QUEUED:
    //_tprintf( TEXT("JobId 0x%x queued\n"),FaxEvent->JobId );
    break; case WinFaxEvent.FEI_DIALING:
    case WinFaxEvent.FEI_SENDING:
    case WinFaxEvent.FEI_RECEIVING:
    case WinFaxEvent.FEI_BUSY:
    case WinFaxEvent.FEI_NO_ANSWER:
    case WinFaxEvent.FEI_BAD_ADDRESS:
    case WinFaxEvent.FEI_NO_DIAL_TONE:
    case WinFaxEvent.FEI_DISCONNECTED:
    case WinFaxEvent.FEI_FATAL_ERROR:
    if (FaxOpenPort(hFax, faxEvent.DeviceId, (int) FAX_PORT_OPEN_TYPE.PORT_OPEN_QUERY, hPort) )
    {
    if (FaxGetDeviceStatus(hPort, out deviceStatus) )
    {
    IntPtr pDeviceStatus = new IntPtr();
    deviceStatus.SizeOfStruct = Marshal.SizeOf(typeof(FAX_DEVICE_STATUS));
    Marshal.StructureToPtr(deviceStatus, pDeviceStatus, true); //PrintDeviceStatus(deviceStatus);
    FaxFreeBuffer(pDeviceStatus);
    }
    FaxClose(hPort);
    }
    break;
    }
    } FaxClose(hFax);
    Win32.CloseHandle(hCompletionPort);
    return true;
    }}
      

  8.   

    你把所有用struct定义的数据类型,改成用class定义,然后用
    //If you use FAX_JOB_PARAM and FAX_COVERPAGE_INFO as a CLASS !
    [DllImport("winfax.dll", SetLastError = true)]
    [DllImport(DLLWinFax, SetLastError = true)]
    static extern bool FaxSendDocument(IntPtr FaxHandle, string FileName,
    FAX_JOB_PARAM JobParams, FAX_COVERPAGE_INFO CoverpageInfo,
    out uint FaxJobId);那么给一个空的对象,可以用null来试。