PInvoke RAS problem

G

Guest

Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " & ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
G

Guest

Thanks Chris,

but i had used Integer, with the same result.
Native Exception: 0xc0000005

Also, if i dont send DWORD

DWORD WINAPI EnumEntries(LPWSTR lpstr)

i have a error in line

wcscpy(lpstr,szTmp);

My device is a Symbol PPC 2003.
Dll is developed with evc++ 4.0

Thanks.


Long in CF is 64-bit. In unmanaged code it's 32-bit. Use Integer instead

-Chris


ACP said:
Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
A

Alex Feinman [MVP]

Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper
 
G

Guest

Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

Alex Feinman said:
Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org
ACP said:
Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
G

Guest

Post a *complete* code snippet. There's something wrong with what you're
doing.

-Chris


ACP said:
Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

Alex Feinman said:
Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org
ACP said:
Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
G

Guest

Here is all my code.

I'm begining and the DLL only has one method.

Unmanaged Code

// RasConnect.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
//#include "stdlib.h"
//#include "tchar.h"
#include "ras.h"
#include "raserror.h"



BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}



DWORD EntradasRAS(LPWSTR lpstr, DWORD &count)
{
LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];


wcscpy(szTmp,_T(""));
//lpRasEntry = '\0';
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
MessageBox(NULL,lpRasEntry[dw].szEntryName,_T("Conexiones"),MB_OK |
MB_ICONWARNING);
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}

Managed code (VB)

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ConexionRAS

#Region "Funciones P/Invoke RasConnect.dll"

<DllImport("RasConnect.dll")> _
Private Function EntradasRAS(ByVal value As StringBuilder, ByRef count
As Integer) As Integer
End Function

#End Region

Public Function GetEntradasRAS() As String
Dim lpszValue As New StringBuilder(255)
Dim dwCount As Integer
Dim ret As Integer

Try
MessageBox.Show("Antes")
ret = Me.EntradasRAS(lpszValue, dwCount)
MessageBox.Show("Despues")

If ret = 0 Then
If dwCount = 0 Then
GetEntradasRAS = ""
Else
GetEntradasRAS = lpszValue.ToString()
End If
Else
MsgBox("Error :" & ret, vbCritical, "Enumeration Entries")
GetEntradasRAS = ""
End If

Catch ex As Exception
MessageBox.Show("Error EnumDevicesRAS: " & ex.Message.ToString())
End Try

End Function

End Class


Result:
Native Exception (0xc0000005)

Thanks


Post a *complete* code snippet. There's something wrong with what you're
doing.

-Chris


ACP said:
Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

Alex Feinman said:
Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org
Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
S

Sergey Bogdanov

As I see, you are concatenating all entries name into szTmp and its size
is 255. Try to enlarge this buffer, it seems that you has the stack
overlfow.

PS:
And should say that it's a bad practice to store such buffers in the
stack... and instead of wcscat use wcsncat, see
(http://msdn.microsoft.com/library/d...tml/_crt_strncat.2c_.wcsncat.2c_._mbsncat.asp)

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Here is all my code.

I'm begining and the DLL only has one method.

Unmanaged Code

// RasConnect.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
//#include "stdlib.h"
//#include "tchar.h"
#include "ras.h"
#include "raserror.h"



BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}



DWORD EntradasRAS(LPWSTR lpstr, DWORD &count)
{
LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];


wcscpy(szTmp,_T(""));
//lpRasEntry = '\0';
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
MessageBox(NULL,lpRasEntry[dw].szEntryName,_T("Conexiones"),MB_OK |
MB_ICONWARNING);
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}

Managed code (VB)

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ConexionRAS

#Region "Funciones P/Invoke RasConnect.dll"

<DllImport("RasConnect.dll")> _
Private Function EntradasRAS(ByVal value As StringBuilder, ByRef count
As Integer) As Integer
End Function

#End Region

Public Function GetEntradasRAS() As String
Dim lpszValue As New StringBuilder(255)
Dim dwCount As Integer
Dim ret As Integer

Try
MessageBox.Show("Antes")
ret = Me.EntradasRAS(lpszValue, dwCount)
MessageBox.Show("Despues")

If ret = 0 Then
If dwCount = 0 Then
GetEntradasRAS = ""
Else
GetEntradasRAS = lpszValue.ToString()
End If
Else
MsgBox("Error :" & ret, vbCritical, "Enumeration Entries")
GetEntradasRAS = ""
End If

Catch ex As Exception
MessageBox.Show("Error EnumDevicesRAS: " & ex.Message.ToString())
End Try

End Function

End Class


Result:
Native Exception (0xc0000005)

Thanks


Post a *complete* code snippet. There's something wrong with what you're
doing.

-Chris


Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

:


Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org

Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
G

Guest

Hi Sergey,

thanks for the link, but the problem is throw by the second parameter (DWORD).
I have execute this simple code with the same result.

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}

void Prueba(DWORD &numero)
{
//DWORD minumero = 5;
//numero = minumero;

numero = 5;
}


Imports System.Runtime.InteropServices

Public Class PruebaPInvoke

#Region "Funciones P/Invoke Pruebas.dll"

<DllImport("Pruebas.dll")> _
Private Sub Prueba(ByRef numero As Integer)
End Sub

#End Region

Public Function GetNumero() As Integer
Dim numeroprueba As Integer = 0

Try
Me.Prueba(numeroprueba)
MessageBox.Show(numeroprueba.ToString())

Catch ex As Exception
MessageBox.Show("Error GetNumero: " & ex.Message.ToString())
End Try

End Function

End Class

What is forgetting to me?

Thanks


Sergey Bogdanov said:
As I see, you are concatenating all entries name into szTmp and its size
is 255. Try to enlarge this buffer, it seems that you has the stack
overlfow.

PS:
And should say that it's a bad practice to store such buffers in the
stack... and instead of wcscat use wcsncat, see
(http://msdn.microsoft.com/library/d...tml/_crt_strncat.2c_.wcsncat.2c_._mbsncat.asp)

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Here is all my code.

I'm begining and the DLL only has one method.

Unmanaged Code

// RasConnect.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
//#include "stdlib.h"
//#include "tchar.h"
#include "ras.h"
#include "raserror.h"



BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}



DWORD EntradasRAS(LPWSTR lpstr, DWORD &count)
{
LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];


wcscpy(szTmp,_T(""));
//lpRasEntry = '\0';
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
MessageBox(NULL,lpRasEntry[dw].szEntryName,_T("Conexiones"),MB_OK |
MB_ICONWARNING);
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}

Managed code (VB)

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ConexionRAS

#Region "Funciones P/Invoke RasConnect.dll"

<DllImport("RasConnect.dll")> _
Private Function EntradasRAS(ByVal value As StringBuilder, ByRef count
As Integer) As Integer
End Function

#End Region

Public Function GetEntradasRAS() As String
Dim lpszValue As New StringBuilder(255)
Dim dwCount As Integer
Dim ret As Integer

Try
MessageBox.Show("Antes")
ret = Me.EntradasRAS(lpszValue, dwCount)
MessageBox.Show("Despues")

If ret = 0 Then
If dwCount = 0 Then
GetEntradasRAS = ""
Else
GetEntradasRAS = lpszValue.ToString()
End If
Else
MsgBox("Error :" & ret, vbCritical, "Enumeration Entries")
GetEntradasRAS = ""
End If

Catch ex As Exception
MessageBox.Show("Error EnumDevicesRAS: " & ex.Message.ToString())
End Try

End Function

End Class


Result:
Native Exception (0xc0000005)

Thanks


Post a *complete* code snippet. There's something wrong with what you're
doing.

-Chris



Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

:


Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org

Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
S

Sergey Bogdanov

You have forgot about extern keyword and dllexport in eVC side:

Pruebas.cpp:

extern "C" {
__declspec(dllexport) void Prueba(DWORD &numero)
{
numero = 5;
}
}

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

for VB.NET forgot about Shared keyword:

<DllImport("Pruebas.dll")>
Private Shared Sub Prueba(ByRef numero As Integer)


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Hi Sergey,

thanks for the link, but the problem is throw by the second parameter (DWORD).
I have execute this simple code with the same result.

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}

void Prueba(DWORD &numero)
{
//DWORD minumero = 5;
//numero = minumero;

numero = 5;
}


Imports System.Runtime.InteropServices

Public Class PruebaPInvoke

#Region "Funciones P/Invoke Pruebas.dll"

<DllImport("Pruebas.dll")> _
Private Sub Prueba(ByRef numero As Integer)
End Sub

#End Region

Public Function GetNumero() As Integer
Dim numeroprueba As Integer = 0

Try
Me.Prueba(numeroprueba)
MessageBox.Show(numeroprueba.ToString())

Catch ex As Exception
MessageBox.Show("Error GetNumero: " & ex.Message.ToString())
End Try

End Function

End Class

What is forgetting to me?

Thanks


:

As I see, you are concatenating all entries name into szTmp and its size
is 255. Try to enlarge this buffer, it seems that you has the stack
overlfow.

PS:
And should say that it's a bad practice to store such buffers in the
stack... and instead of wcscat use wcsncat, see
(http://msdn.microsoft.com/library/d...tml/_crt_strncat.2c_.wcsncat.2c_._mbsncat.asp)

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Here is all my code.

I'm begining and the DLL only has one method.

Unmanaged Code

// RasConnect.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
//#include "stdlib.h"
//#include "tchar.h"
#include "ras.h"
#include "raserror.h"



BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}



DWORD EntradasRAS(LPWSTR lpstr, DWORD &count)
{
LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];


wcscpy(szTmp,_T(""));
//lpRasEntry = '\0';
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
MessageBox(NULL,lpRasEntry[dw].szEntryName,_T("Conexiones"),MB_OK |
MB_ICONWARNING);
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}

Managed code (VB)

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ConexionRAS

#Region "Funciones P/Invoke RasConnect.dll"

<DllImport("RasConnect.dll")> _
Private Function EntradasRAS(ByVal value As StringBuilder, ByRef count
As Integer) As Integer
End Function

#End Region

Public Function GetEntradasRAS() As String
Dim lpszValue As New StringBuilder(255)
Dim dwCount As Integer
Dim ret As Integer

Try
MessageBox.Show("Antes")
ret = Me.EntradasRAS(lpszValue, dwCount)
MessageBox.Show("Despues")

If ret = 0 Then
If dwCount = 0 Then
GetEntradasRAS = ""
Else
GetEntradasRAS = lpszValue.ToString()
End If
Else
MsgBox("Error :" & ret, vbCritical, "Enumeration Entries")
GetEntradasRAS = ""
End If

Catch ex As Exception
MessageBox.Show("Error EnumDevicesRAS: " & ex.Message.ToString())
End Try

End Function

End Class


Result:
Native Exception (0xc0000005)

Thanks


:



Post a *complete* code snippet. There's something wrong with what you're
doing.

-Chris




Thanks Alex,

but there has been no luck. I have a fatal exception 0x80000002 (Out of
Memory)
in line

wcscpy(lpstr,szTmp);

I dont know what is happening. Any idea is good received!!

Thanks.

:



Try zeroing the buffer you allocated for lpRasEntry before using it.
On the approach - it's your choice, but I find it much easier to P/Invoke
RasEnumEntries directly, without ever writing an unmanaged wrapper

--
Alex Feinman
---
Visit http://www.opennetcf.org


Hi group,

I have to pinvoke a RAS dll, but i have a native exception and
a dont know what are i doing incorret.

Unmanaged Code:
------------------------

DWORD WINAPI EnumEntries(LPWSTR lpstr, DWORD &count)
{

LPRASENTRYNAME lpRasEntry = NULL;
DWORD dwRes, dwSize, dwEntries, dw;
WCHAR szTmp[255];

wcscpy(szTmp,_T(""));
lpRasEntry = new RASENTRYNAME[20];
if(lpRasEntry == NULL)
{
return 1;
}
lpRasEntry[0].dwSize = sizeof(RASENTRYNAME);
dwSize = sizeof(RASENTRYNAME) * 20;
dwRes = RasEnumEntries(NULL, NULL, lpRasEntry, &dwSize, &dwEntries);

if (dwRes == 0)
{
count = dwEntries;
for(dw = 0; dw < dwEntries; dw++)
{
wcscat(szTmp,lpRasEntry[dw].szEntryName);
if (dw != dwEntries)
wcscat(szTmp,_T(","));
}

//wcsncpy(lpstr, szTmp, 255);
wcscpy(lpstr,szTmp);
}

delete [] lpRasEntry;
return dwRes;
}


Managed Code (VB):
------------------------

<DllImport("DllPrueba.dll")> _
Private Function EnumEntries(ByVal lpszValue As StringBuilder, ByRef
dwCount as Long) As Long
End Function

...........

Dim szValue As New StringBuilder(255)
Dim dwCount As Long
Dim ret As Long

Try

ret = EnumEntries(szValue, dwCount)

MessageBox.Show("Resultado: " & szValue.toString())

Catch ex As Exception
MessageBox.Show("Error EnumEntriesRAS: " &
ex.Message.ToString())
End Try

I'm using StringBuilder with LPWSTR and ByRef with DWORD&.
Where they are the error(s)?

Thanks.
 
G

Guest

Hi Sergery,

thanks, it is running!!.

But I do not understand where is the problem. I have a .def file where i
export my methods. I have always thought, that .def file and extern clause
were equivalent.

Where i can find information about it??

Thanks to all for your responses.
 
G

Guest

Hi ACP,

I'd like to have a copy of your RAS Dll if you don't mind. It's for personal
use. My e-mail is (e-mail address removed) .

Thanks in advance.
 
G

Guest

Hi Ray,

I developed the dll but it is property of our client, I am sorry.

Your device is PPC or WinCE??
What funcionality are you looking for??
In our dll we used, in addition to RAS, connection manager and RIL. I (and
this group) can help you to develop your own dll with RAS.

If it can help you, you have small RAS DLLs here:

http://www.codeppc.com/evc/index.htm

Once again, i am sorry. Excuse me.

Regards

ACP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top