lineInitializeEx works with C# but NotSupportedException in VB.Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having this weird problem of NotSupportedException when I am trying to
use lineInitializeEx in a PocketPC 2003 application with .Net Compact
Framework
2.0. It works perfectly alright with C# but gives a NotSupportedException in
VB.NET.

The code for that I am using (for C# as well as VB.Net) is given below. I am
at the end of my wits trying to figure out why the VB.Net code fails although
it is roughly same as C# code.

===================================
C# Code (Works Without Problem)
===================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CSharpPPC
{
public partial class frmMain : Form
{
public struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public System.IntPtr hEvent;
public uint dwCompletionKey;
}

public enum LineErrReturn : uint
{
LINE_OK = 0x00000000,
LINEERR_INVALAPPNAME = 0x80000015,
LINEERR_OPERATIONFAILED = 0x80000048,
LINEERR_INIFILECORRUPT = 0x8000000E,
LINEERR_INVALPOINTER = 0x80000035,
LINEERR_REINIT = 0x80000052,
LINEERR_NOMEM = 0x80000044,
LINEERR_INVALPARAM = 0x80000032
}

public enum LINEINITIALIZEEXOPTION
{
LINEINITIALIZEEXOPTION_USECOMPLETIONPORT = 3,
LINEINITIALIZEEXOPTION_USEEVENT = 2,
LINEINITIALIZEEXOPTION_USEHIDDENWINDOW = 1
}

[DllImport("coredll.dll",SetLastError=true)]
internal static extern LineErrReturn lineInitializeEx(
out IntPtr hLineApp,
IntPtr hAppHandle,
IntPtr lCallBack,
string FriendlyAppName,
out System.UInt32 NumDevices,
ref System.UInt32 APIVersion,
ref LINEINITIALIZEEXPARAMS lineExInitParams);

public frmMain()
{
InitializeComponent();
}

private void mnuItemQuit_Click(object sender, EventArgs e)
{
Close();
}

private void mnuItemInfo_Click(object sender, EventArgs e)
{
try
{
UInt32 TAPI_API_HIGH_VERSION = 0x00020000;
IntPtr m_hLineApp = IntPtr.Zero;
UInt32 m_TapiVersion = TAPI_API_HIGH_VERSION;
UInt32 m_lpdwNumDevs;
IntPtr m_AppHandle = IntPtr.Zero;

// prepare the lineinitializeexparams
LINEINITIALIZEEXPARAMS liep = new LINEINITIALIZEEXPARAMS();
liep.dwTotalSize = (uint)Marshal.SizeOf(liep);
liep.dwNeededSize = liep.dwTotalSize;
liep.dwUsedSize = liep.dwTotalSize;
liep.hEvent = System.IntPtr.Zero;
liep.dwOptions =
(uint)LINEINITIALIZEEXOPTION.LINEINITIALIZEEXOPTION_USEEVENT;

LineErrReturn RetVal = lineInitializeEx(out m_hLineApp,
m_AppHandle, IntPtr.Zero, "Test", out m_lpdwNumDevs, ref m_TapiVersion, ref
liep);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
=========END OF C# CODE===============


===================================
VB.Net Code (Throws NotSupportedException)
===================================
Imports System.Runtime.InteropServices

Public Class frmMain

Public Structure LINEINITIALIZEEXPARAMS
Public dwTotalSize As Integer
Public dwNeededSize As Integer
Public dwUsedSize As Integer
Public dwOptions As Integer
Public hEvent As IntPtr
Public dwCompletionKey As Integer
End Structure

Public Enum LineErrReturn As Integer
LINE_OK = &H0
LINEERR_INVALAPPNAME = &H80000015
LINEERR_OPERATIONFAILED = &H80000048
LINEERR_INIFILECORRUPT = &H8000000E
LINEERR_INVALPOINTER = &H80000035
LINEERR_REINIT = &H80000052
LINEERR_NOMEM = &H80000044
LINEERR_INVALPARAM = &H80000032
End Enum

Public Enum LINEINITIALIZEEXOPTION
LINEINITIALIZEEXOPTION_USECOMPLETIONPORT = 3
LINEINITIALIZEEXOPTION_USEEVENT = 2
LINEINITIALIZEEXOPTION_USEHIDDENWINDOW = 1
End Enum

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function lineInitializeEx( _
ByRef lphLineApp As IntPtr, _
ByVal hInstance As IntPtr, _
ByVal lpfnCallback As IntPtr, _
ByVal lpszFriendlyAppName As String, _
ByRef lpdwNumDevs As UInt32, _
ByRef lpdwAPIVersion As UInt32, _
ByRef lpLineInitializeExParams As LINEINITIALIZEEXPARAMS) As Long
End Function

Private Sub mnuItemInfo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuItemInfo.Click
Try
Dim TAPI_API_HIGH_VERSION As UInt32 = &H20000
Dim m_hLineApp As IntPtr = IntPtr.Zero
Dim m_TapiVersion As UInt32 = TAPI_API_HIGH_VERSION
Dim m_lpdwNumDevs As UInt32
Dim m_AppHandle As IntPtr = IntPtr.Zero

'prepare the lineinitializeexparams
Dim liep As LINEINITIALIZEEXPARAMS = New LINEINITIALIZEEXPARAMS
liep.dwTotalSize = Marshal.SizeOf(liep)
liep.dwNeededSize = liep.dwTotalSize
liep.dwUsedSize = liep.dwTotalSize
liep.hEvent = System.IntPtr.Zero
liep.dwOptions =
LINEINITIALIZEEXOPTION.LINEINITIALIZEEXOPTION_USEEVENT

Dim RetVal As LineErrReturn = lineInitializeEx(m_hLineApp,
m_AppHandle, IntPtr.Zero, "Test", m_lpdwNumDevs, m_TapiVersion, liep)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub mnuItemQuit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuItemQuit.Click
Application.Exit()
End Sub
End Class
=======END OF VB.NET CODE=============

Anyone out there who can help!!!

Thanks

Santanu Biswas
 
When you have something that works in C# and doesn't in VB.NET, do a search
in your VB.NET code for "long". Long in VB.NET is 64 bits, but "long" in
C/C++ is 32-bits, so you'll often accidentally declare something as taking a
64 bit integer parameter in VB.NET, which doesn't work.

Paul T.

Santanu Biswas said:
I am having this weird problem of NotSupportedException when I am trying to
use lineInitializeEx in a PocketPC 2003 application with .Net Compact
Framework
2.0. It works perfectly alright with C# but gives a NotSupportedException
in
VB.NET.

The code for that I am using (for C# as well as VB.Net) is given below. I
am
at the end of my wits trying to figure out why the VB.Net code fails
although
it is roughly same as C# code.

===================================
C# Code (Works Without Problem)
===================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CSharpPPC
{
public partial class frmMain : Form
{
public struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public System.IntPtr hEvent;
public uint dwCompletionKey;
}

public enum LineErrReturn : uint
{
LINE_OK = 0x00000000,
LINEERR_INVALAPPNAME = 0x80000015,
LINEERR_OPERATIONFAILED = 0x80000048,
LINEERR_INIFILECORRUPT = 0x8000000E,
LINEERR_INVALPOINTER = 0x80000035,
LINEERR_REINIT = 0x80000052,
LINEERR_NOMEM = 0x80000044,
LINEERR_INVALPARAM = 0x80000032
}

public enum LINEINITIALIZEEXOPTION
{
LINEINITIALIZEEXOPTION_USECOMPLETIONPORT = 3,
LINEINITIALIZEEXOPTION_USEEVENT = 2,
LINEINITIALIZEEXOPTION_USEHIDDENWINDOW = 1
}

[DllImport("coredll.dll",SetLastError=true)]
internal static extern LineErrReturn lineInitializeEx(
out IntPtr hLineApp,
IntPtr hAppHandle,
IntPtr lCallBack,
string FriendlyAppName,
out System.UInt32 NumDevices,
ref System.UInt32 APIVersion,
ref LINEINITIALIZEEXPARAMS lineExInitParams);

public frmMain()
{
InitializeComponent();
}

private void mnuItemQuit_Click(object sender, EventArgs e)
{
Close();
}

private void mnuItemInfo_Click(object sender, EventArgs e)
{
try
{
UInt32 TAPI_API_HIGH_VERSION = 0x00020000;
IntPtr m_hLineApp = IntPtr.Zero;
UInt32 m_TapiVersion = TAPI_API_HIGH_VERSION;
UInt32 m_lpdwNumDevs;
IntPtr m_AppHandle = IntPtr.Zero;

// prepare the lineinitializeexparams
LINEINITIALIZEEXPARAMS liep = new LINEINITIALIZEEXPARAMS();
liep.dwTotalSize = (uint)Marshal.SizeOf(liep);
liep.dwNeededSize = liep.dwTotalSize;
liep.dwUsedSize = liep.dwTotalSize;
liep.hEvent = System.IntPtr.Zero;
liep.dwOptions =
(uint)LINEINITIALIZEEXOPTION.LINEINITIALIZEEXOPTION_USEEVENT;

LineErrReturn RetVal = lineInitializeEx(out m_hLineApp,
m_AppHandle, IntPtr.Zero, "Test", out m_lpdwNumDevs, ref m_TapiVersion,
ref
liep);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
=========END OF C# CODE===============


===================================
VB.Net Code (Throws NotSupportedException)
===================================
Imports System.Runtime.InteropServices

Public Class frmMain

Public Structure LINEINITIALIZEEXPARAMS
Public dwTotalSize As Integer
Public dwNeededSize As Integer
Public dwUsedSize As Integer
Public dwOptions As Integer
Public hEvent As IntPtr
Public dwCompletionKey As Integer
End Structure

Public Enum LineErrReturn As Integer
LINE_OK = &H0
LINEERR_INVALAPPNAME = &H80000015
LINEERR_OPERATIONFAILED = &H80000048
LINEERR_INIFILECORRUPT = &H8000000E
LINEERR_INVALPOINTER = &H80000035
LINEERR_REINIT = &H80000052
LINEERR_NOMEM = &H80000044
LINEERR_INVALPARAM = &H80000032
End Enum

Public Enum LINEINITIALIZEEXOPTION
LINEINITIALIZEEXOPTION_USECOMPLETIONPORT = 3
LINEINITIALIZEEXOPTION_USEEVENT = 2
LINEINITIALIZEEXOPTION_USEHIDDENWINDOW = 1
End Enum

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function lineInitializeEx( _
ByRef lphLineApp As IntPtr, _
ByVal hInstance As IntPtr, _
ByVal lpfnCallback As IntPtr, _
ByVal lpszFriendlyAppName As String, _
ByRef lpdwNumDevs As UInt32, _
ByRef lpdwAPIVersion As UInt32, _
ByRef lpLineInitializeExParams As LINEINITIALIZEEXPARAMS) As Long
End Function

Private Sub mnuItemInfo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuItemInfo.Click
Try
Dim TAPI_API_HIGH_VERSION As UInt32 = &H20000
Dim m_hLineApp As IntPtr = IntPtr.Zero
Dim m_TapiVersion As UInt32 = TAPI_API_HIGH_VERSION
Dim m_lpdwNumDevs As UInt32
Dim m_AppHandle As IntPtr = IntPtr.Zero

'prepare the lineinitializeexparams
Dim liep As LINEINITIALIZEEXPARAMS = New LINEINITIALIZEEXPARAMS
liep.dwTotalSize = Marshal.SizeOf(liep)
liep.dwNeededSize = liep.dwTotalSize
liep.dwUsedSize = liep.dwTotalSize
liep.hEvent = System.IntPtr.Zero
liep.dwOptions =
LINEINITIALIZEEXOPTION.LINEINITIALIZEEXOPTION_USEEVENT

Dim RetVal As LineErrReturn = lineInitializeEx(m_hLineApp,
m_AppHandle, IntPtr.Zero, "Test", m_lpdwNumDevs, m_TapiVersion, liep)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub mnuItemQuit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuItemQuit.Click
Application.Exit()
End Sub
End Class
=======END OF VB.NET CODE=============

Anyone out there who can help!!!

Thanks

Santanu Biswas
 
Sorry for the delay in my response.
That was absolutely a great tip and solved my problem. The problem was in
DllImport of coredll.dll
I simply changed the return type as Integer (instead of Long) and it worked:-)
(actually the return type should more appropriately be of type LineErrReturn).

Thanks a ton for your help.
 
Back
Top