Hello. I am develop a console application that needs to interact with a 3rd
party API that I believe was written in VB6. The program works fine and
executes as expected, but when my program exits, I get an error that memory
could not be read. I've searched around for the past few days with little
luck. Anyone have any ideas or could point me in the right direction? My
code is as follows.
Thanks,
Jason Chrin
---------------------------------------------
using System;
using System.Runtime.InteropServices;
namespace IOTest
{
/// <summary>
/// Holds the calls for the P/Invoke methods
/// </summary>
public class Externals{
// NOTES:
// Long type(Vb6 32bit int) must be Marshalled to an Int32 using
UnmanagedType.I4
// String type is Marshalled using UnmanagedType.BStr
[DllImport(@"dcapi.dll")]public static extern int
dc_connect_mssql([MarshalAs (UnmanagedType.AnsiBStr)]string dburl,
[MarshalAs(UnmanagedType.I4)]Int32 dbport,
[MarshalAs(UnmanagedType.BStr)]string dbun,
[MarshalAs(UnmanagedType.BStr)]string dbpass,
[MarshalAs(UnmanagedType.BStr)]string dcun,
[MarshalAs(UnmanagedType.BStr)]string dcpass,
[MarshalAs(UnmanagedType.BStr)]string dcschema,
[MarshalAs(UnmanagedType.I4)]Int32 authentication,
[MarshalAs(UnmanagedType.I4)]Int32 usePipes);
[DllImport(@"dcapi.dll")]public static extern int dc_disconnect();
[DllImport(@"dcapi.dll")]public static extern int
dc_getlasterror([MarshalAs(UnmanagedType.BStr)]ref string pszOut,
[MarshalAs(UnmanagedType.I4)]Int32 pszOutLen);
}
/// <summary>
/// Main Program
/// </summary>
public class MainProg{
private const Int32 portNo = 1433;
private const Int32 messageLength = 100;
[STAThread]
static void Main(string[] args)
{
string outValue = string.Empty;
Int32 AccessType = 0;
Int32 UsePipes = 0;
int returnValue = Externals.dc_connect_mssql("localhost", portNo,
"dbuser", "dbpassword", "dcuser", "dcpassword", "schema", AccessType,
UsePipes);
Externals.dc_getlasterror(ref outValue, messageLength);
System.Console.WriteLine(returnValue.ToString());
Externals.dc_disconnect();
}
}
}
Mattias Sjögren - 30 Mar 2006 17:16 GMT
Jason,
>Hello. I am develop a console application that needs to interact with a 3rd
>party API that I believe was written in VB6.
I doubt it, since VB doesn't support exporting functions that can be
called through PInvoke.
Where did you get the function signatures from to write the
declarations?
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jason Chrin - 30 Mar 2006 19:38 GMT
The api came with a docment listing all of the methods and arguments. The
integers were listed as long, so I assumed it would be vb6 code. I tried
adding the dll as a reference in the project, but it said it was not a valid
com file. Using P/Invoke I was able to call the functions, but I get the
memory error when my program exits.
Thank you,
Jason
> Jason,
>
[quoted text clipped - 8 lines]
>
> Mattias