Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i am converting your vb code into c# but having error kindly guide me if i am doing something wrong.

////////////vb code///////////////
VB
Public Declare Function IO_HasScanner Lib "scandll.dll" (ByVal IVS_600DS As String, ByVal hHandle As Long) As Long

Private Sub CmdConnect_Click()
Dim lRet As Long

lRet = IO_HasScanner("IDCapture", Me.hwnd)
If lRet = 0 Then
    Text1.Text = "Scanner connected."
    bLoaded = True
    CmdConnect.Enabled = False
    OptionManual.Value = True
    strGreyImageFile = "G01.bmp"
    strColorImageFile = "C01.bmp"
    strGreyImageFile_Head = "GH01.bmp"
    strColorImageFile_Head = "CH01.bmp"
Else
    Text1.Text = "Device not connected."
    bLoaded = False
    CmdConnect.Enabled = True
End If
End Sub


/////////c# 4.0 code////////

C#
[DllImport("ScanDll.dll")]
        public static extern long IO_HasScanner(string IVS_600DS, long hHandle);

        private void Form1_Load(object sender, EventArgs e)
        {
            long result;
            result = IO_HasScanner("IDCapture", this.Handle.ToInt32()); //// Get Error on this line.
            MessageBox.Show(result.ToString());
        }
Posted
Updated 14-Jun-14 4:09am
v2
Comments
CPallini 14-Jun-14 10:12am    
What kind of error are you getting?
Farhan Qureshi 14-Jun-14 10:16am    
A call to PInvoke function 'scanTest!scanTest.Form1::IO_HasScanner' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
[no name] 14-Jun-14 10:13am    
Well there are several things wrong here. You could not possibly be porting any VB6 code that I wrote to C#. You should not be porting VB6 code to .NET, you should be rewriting it. And we could not possibly know which error it is that you are seeing out of the hundreds of errors that it could be.
Farhan Qureshi 14-Jun-14 10:16am    
A call to PInvoke function 'scanTest!scanTest.Form1::IO_HasScanner' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
[no name] 14-Jun-14 10:24am    
"Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

Well yes - you will.
A long is not the same as an Int32 - it's an Int64
Try:
C#
long result;
result = IO_HasScanner("IDCapture", this.Handle.ToInt64());
 
Share this answer
 
Comments
Farhan Qureshi 14-Jun-14 10:19am    
A call to PInvoke function 'scanTest!scanTest.Form1::IO_HasScanner' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
CPallini 14-Jun-14 10:45am    
Well, a VB6 Long is not exactly 64 bit wide. :-)
Dave Kreskowiak 14-Jun-14 11:09am    
You might want to be a bit more specific.

A Long in VB6 is an Int32 in C#.
Farhan Qureshi 14-Jun-14 11:34am    
http://fileflyer.com/view/Foa2FAW
Dave Kreskowiak 14-Jun-14 14:17pm    
...and what is this link for?

No, I'm not clicking on it.
No, I'm downloading your project if that is what this is.
Maybe you can try this now - long changed to Int32

C#
[DllImport("ScanDll.dll")]
public static extern Int32 IO_HasScanner([MarshalAs(UnmanagedType.VBByRefStr)] ref string IVS_600DS, Int32 hHandle);

private void Form1_Load(object sender, EventArgs e)
{
   Int32 result;
   result = IO_HasScanner("IDCapture", this.Handle.ToInt32()); 
   MessageBox.Show(result.ToString());
}
 
Share this answer
 
v3
Comments
Farhan Qureshi 14-Jun-14 10:31am    
still same error

[DllImport("ScanDll.dll")]
public static extern long IO_HasScanner([MarshalAs(UnmanagedType.VBByRefStr)] ref string IVS_600DS, long hHandle);

private void Form1_Load(object sender, EventArgs e)
{
long result;
string txt = "IDCapture";
result = IO_HasScanner(ref txt, this.Handle.ToInt64());
MessageBox.Show(result.ToString());
}
TheUnknownCoder 14-Jun-14 10:49am    
see new solution
Farhan Qureshi 14-Jun-14 10:51am    
still same
:(
Farhan Qureshi 14-Jun-14 10:52am    
[DllImport(@"ScanDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern long IO_HasScanner([MarshalAs(UnmanagedType.VBByRefStr)] ref string IVS_600DS, Int32 hHandle);

private void Form1_Load(object sender, EventArgs e)
{
long result;
string txt = "IDCapture";
result = IO_HasScanner(ref txt, this.Handle.ToInt32());
MessageBox.Show(result.ToString());
}
TheUnknownCoder 14-Jun-14 10:53am    
sorry, can´t try it, no scanner .... and no scandll.dll
just data type mismatch

i was using long instead of int32.

C#
[DllImport(@"ScanDll.dll")]
public static extern Int32 IO_HasScanner(string IVS_600DS, int hHandle);

private void Form1_Load(object sender, EventArgs e)
{
    long result;
    result = IO_HasScanner("IDCapture", this.Handle.ToInt32());
    MessageBox.Show(result.ToString());
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900