lundi 19 mai 2014

Convertir code Declare Function de vb.net à c# - Stack Overflow


This is the complete vb.net source code.


Imports System.Runtime.InteropServices
Imports System.IO

Public Class WebCam

'WEb camera constants'
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000

Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Const WM_CAP_SEQUENCE = WM_CAP_START + 62
Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23

Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50

Const SWP_NOMOVE = &H2S
Const SWP_NOSIZE = 1
Const SWP_NOZORDER = &H4S
Const HWND_BOTTOM = 1

'--The capGetDriverDescription function retrieves the version description of the capture driver--'
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
(ByVal wDriverIndex As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean

'--The capCreateCaptureWindow function creates a capture window--'
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Short, ByVal hWnd As Integer, _
ByVal nID As Integer) As Integer

'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

'--Sets the position of the window relative to the screen buffer--'
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

'--This function destroys the specified window--'
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean

Dim VideoSource As Integer
Dim hWnd As Integer
'__________________________________________________ End of web camera'

'web cam subs'
'--disconnect from video source---'
Private Sub StopPreviewWindow()
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
End Sub

'---list all the various video sources---'
Private Sub ListVideoSources()
lstVideoSources.Items.Clear()
Dim DriverName As String = Space(80)
Dim DriverVersion As String = Space(80)
For i As Integer = 0 To 9
If capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80) Then
lstVideoSources.Items.Add(DriverName.Trim)
End If
Next
End Sub

'---save the image---'
Private Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image
PictureBoxCaptured.Image = bmap

'---copy the image to the clipboard---'
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)

'---retrieve the image from clipboard and convert it '
' to the bitmap format'
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = _
CType(data.GetData(GetType(System.Drawing.Bitmap)), _
Image)
PictureBoxCaptured.Image = bmap
'StopPreviewWindow()'
End If
End Sub


'---preview the selected video source---'
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
0, pbCtrl.Handle.ToInt32, 0)
If SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0) Then
'---set the preview scale---'
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
'---set the preview rate (ms)---'
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
'---start previewing the image---'
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
'---resize window to fit in PictureBox control---'
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
pbCtrl.Width, pbCtrl.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
'--error connecting to video source---'
DestroyWindow(hWnd)
End If
End Sub


Private Sub ButtonWebCamCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamCapture.Click
CaptureImage()
End Sub

Private Sub ButtonWebCamView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamView.Click
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub


Private Sub lstVideoSources_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstVideoSources.SelectedIndexChanged
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Try
StopPreviewWindow()
PictureBoxLive.Image = Nothing
PictureBoxCaptured.Image = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub FormWebCam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListVideoSources()
If (lstVideoSources.Items.Count > 0) Then
lstVideoSources.SelectedIndex = 0
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)

End If

'PictureBoxCaptured.Image.Dispose()'
End Sub

Private Sub ButtonSaveAndExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveAndExit.Click
If (PictureBoxCaptured.Image Is Nothing) Then
If (MessageBox.Show("Image is empty." & vbCrLf & "Do you want to exit ?", "Empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
StopPreviewWindow()
Me.Close()
End If
Else
If (ModuleFunctions.IsFormOpen(Item)) Then
Item.PictureBoxItem.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(Customer)) Then
Customer.PictureBoxCaptured.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(BillCustomises)) Then
BillCustomises.PictureBoxItem.Image = PictureBoxCaptured.Image
End If
StopPreviewWindow()
Me.Close()
End If
End Sub

Private Sub FormWebCam_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Me.Dispose()
End Sub
End Class

Above code is working fine with my old vb.net project. Now I want to convert that project into c#. I couldn't convert only the following function.


I want to convert following vb.net code to c#


'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

I tried using different converters they gives error


"CONVERSION ERROR: Code could not be converted. Details:


-- line 1 col 9: invalid NonModuleDeclaration


Please check for any errors in the original code and try again. "


And I tried my self to convert it, but I have ended up with this.


 //--This function sends the specified message to a window or windows--
[DllImport("user32.dll")]
public static extern Boolean SendMessage(int hwnd, int Msg, int wParam, Object lParam);

But it was not correct.Please help me to solve this issue.




Your C# version should be something like this:


[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg,
IntPtr wParam, IntPtr lParam);



Finally I found the answer with complete list. Thank you every one.


Click here to view complete code


     [DllImport("avicap32.dll")]
protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string
lpszWindowName,int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);


This is the complete vb.net source code.


Imports System.Runtime.InteropServices
Imports System.IO

Public Class WebCam

'WEb camera constants'
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000

Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Const WM_CAP_SEQUENCE = WM_CAP_START + 62
Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23

Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50

Const SWP_NOMOVE = &H2S
Const SWP_NOSIZE = 1
Const SWP_NOZORDER = &H4S
Const HWND_BOTTOM = 1

'--The capGetDriverDescription function retrieves the version description of the capture driver--'
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
(ByVal wDriverIndex As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean

'--The capCreateCaptureWindow function creates a capture window--'
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Short, ByVal hWnd As Integer, _
ByVal nID As Integer) As Integer

'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

'--Sets the position of the window relative to the screen buffer--'
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

'--This function destroys the specified window--'
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean

Dim VideoSource As Integer
Dim hWnd As Integer
'__________________________________________________ End of web camera'

'web cam subs'
'--disconnect from video source---'
Private Sub StopPreviewWindow()
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
End Sub

'---list all the various video sources---'
Private Sub ListVideoSources()
lstVideoSources.Items.Clear()
Dim DriverName As String = Space(80)
Dim DriverVersion As String = Space(80)
For i As Integer = 0 To 9
If capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80) Then
lstVideoSources.Items.Add(DriverName.Trim)
End If
Next
End Sub

'---save the image---'
Private Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image
PictureBoxCaptured.Image = bmap

'---copy the image to the clipboard---'
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)

'---retrieve the image from clipboard and convert it '
' to the bitmap format'
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = _
CType(data.GetData(GetType(System.Drawing.Bitmap)), _
Image)
PictureBoxCaptured.Image = bmap
'StopPreviewWindow()'
End If
End Sub


'---preview the selected video source---'
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
0, pbCtrl.Handle.ToInt32, 0)
If SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0) Then
'---set the preview scale---'
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
'---set the preview rate (ms)---'
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
'---start previewing the image---'
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
'---resize window to fit in PictureBox control---'
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
pbCtrl.Width, pbCtrl.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
'--error connecting to video source---'
DestroyWindow(hWnd)
End If
End Sub


Private Sub ButtonWebCamCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamCapture.Click
CaptureImage()
End Sub

Private Sub ButtonWebCamView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWebCamView.Click
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub


Private Sub lstVideoSources_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstVideoSources.SelectedIndexChanged
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Try
StopPreviewWindow()
PictureBoxLive.Image = Nothing
PictureBoxCaptured.Image = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub FormWebCam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListVideoSources()
If (lstVideoSources.Items.Count > 0) Then
lstVideoSources.SelectedIndex = 0
'---stop video in case it is on---'
StopPreviewWindow()
'---check which video source is selected---'
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source'
PreviewVideo(PictureBoxLive)

End If

'PictureBoxCaptured.Image.Dispose()'
End Sub

Private Sub ButtonSaveAndExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveAndExit.Click
If (PictureBoxCaptured.Image Is Nothing) Then
If (MessageBox.Show("Image is empty." & vbCrLf & "Do you want to exit ?", "Empty", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
StopPreviewWindow()
Me.Close()
End If
Else
If (ModuleFunctions.IsFormOpen(Item)) Then
Item.PictureBoxItem.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(Customer)) Then
Customer.PictureBoxCaptured.Image = PictureBoxCaptured.Image
ElseIf (ModuleFunctions.IsFormOpen(BillCustomises)) Then
BillCustomises.PictureBoxItem.Image = PictureBoxCaptured.Image
End If
StopPreviewWindow()
Me.Close()
End If
End Sub

Private Sub FormWebCam_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Me.Dispose()
End Sub
End Class

Above code is working fine with my old vb.net project. Now I want to convert that project into c#. I couldn't convert only the following function.


I want to convert following vb.net code to c#


'--This function sends the specified message to a window or windows--'
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

I tried using different converters they gives error


"CONVERSION ERROR: Code could not be converted. Details:


-- line 1 col 9: invalid NonModuleDeclaration


Please check for any errors in the original code and try again. "


And I tried my self to convert it, but I have ended up with this.


 //--This function sends the specified message to a window or windows--
[DllImport("user32.dll")]
public static extern Boolean SendMessage(int hwnd, int Msg, int wParam, Object lParam);

But it was not correct.Please help me to solve this issue.



Your C# version should be something like this:


[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg,
IntPtr wParam, IntPtr lParam);


Finally I found the answer with complete list. Thank you every one.


Click here to view complete code


     [DllImport("avicap32.dll")]
protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string
lpszWindowName,int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);

0 commentaires:

Enregistrer un commentaire