Posted By: mohit.chawla | Feb 14th, 2006 @ 1:59 AM
page 1 of 1
Comments: 9 | Views: 6380
mohit.chawla
mohit.chawla
Maverick

Hi,

I am trying to pass an integer value from .NET CF to evc4 ATL Com dll function through <dllImport> attribute. As soon as i Pass this value this value is converted to 0 in evc4. For eg: if i am pass an interger value 7 thorugh .Net CF,  then i receive an interger value 0 in eVC4 dll function.
Can anyone help me find the reason for this behaviour. Below is the signature for both functions at both ends:

 In eVC4 Dll function defination:

#define EXP extern "C" __declspec(dllexport)

EXP int Sample::intValPassTest(int var)
{
 /*   //Code for converting the integer value to string and see it through MessageBox in .Net CF

 TCHAR arr[5];
 wsprintf(arr,TEXT("%d"),var);
 MessageBox(NULL,arr,L"emVC4",NULL);
 */  

 return var;

}  ///Class Sample declaration with all function declarations is given in a Header file which is included in the project.


VB.Net CF Function.

<DllImport("Sample_ATL_COM.dll", CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Auto, EntryPoint:="intValPassTest", SetLastError:=True)> _
    Public Shared Function intValPassTest(ByVal i As Integer) As Integer

    End Function

And I am calling this function on a button_click event. And its code is written below:

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Try
           
            Dim i As Integer = 55
            Dim ii As Integer
            ii = intValPassTest(i)
            MsgBox(ii)

          
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


Any help from your side is invited, thank you in Advance,

mohit chawla

It looks like you're trying to use a class instance member function. This won't work: P/Invoke only works with 'flat' APIs. You should make your C++ function a global-scoped function - your extern "C" declaration will then work and the entry point name will not be decorated.

Technical detail: the C++ member function is expecting the 'this' pointer to be passed as the first argument (in register R0 on an ARM chip), and 'var' as the second argument (register R1). The P/Invoke is passing your value of 55 as the first argument (R0 = 55), and no second argument (R1 keeps whatever value it had). If you actually referenced any instance members in your C++ function, it would almost certainly crash with an access violation or datatype misalignment exception, since address 55 is not a valid pointer. Since you aren't, it simply doesn't work.
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
A long in VB.NET is 64 bits. In regular VC, this is the equivalent of the "long long" or "__int64" data type, NOT "long". In most C++ implementations, both int and long are 32 bits. I assume this applies to eVC4 as well.

Try to use "long long" or "__int64" as the data type on the C++ side, and see if that works.
Hi Sven,

My name is Prateek and I am working with Mohit on the same project.

In eVC4 __int64 is supported. But even after using this I am getting the same error "System.NotSupportedException"


Any suggestions.

Thanks.

Prateek
long can only be marshalled if it's ByRef (ref in C#)

See this MSDN article

Hi Andy,

I have already tried passing long parameter ByRef in VB .Net but it not working and giving me the same "SystemNotSupportedException" exception.

Thanks.

Prateek

 

 

Hacking up the previous example:

C++ bit:

EXP int intValPassTest(__int64* var)
{
    return (int) *var;
}

VB bit:
    <DllImport("mydll.dll")> _
    Private Shared Function intValPassTest(ByRef var As Long) As Integer
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        MessageBox.Show(intValPassTest(55).ToString())

    End Sub

Thanks Mike thanks.
Your comments helped me a lot.

Mike I have another problem in passing a string from .Net CF to eVC4 Dll which I have already posted as a seperate query.

please help me out in that also.

Thanks.

Prateek