Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2006

Tip: Looking for answers? Try searching our database.

Memory couldn not be read vb6 interop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason Chrin - 30 Mar 2006 15:49 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.