Calling a C-style DLL from C# with only a VB example of how it should work

Hi, I'm just getting started with Interop, and I would appreciate any help you might give me on this question (and others upcoming, I'm sure).

I have been assigned a project which requires me to interact with a regular non-COM DLL.  I don't have any definitions for the library functions, but I have detailed VB examples of how to work with the DLL.  All the documentation regarding converting/marshaling types i've seen requires knowing the original types in the DLL.  I do not have this information.  I have tons of VB examples from the company's SDK, but none that show how to use any .NET language.

The SDK shows this in the C++ bvtypes.h file:
typedef struct {
      char Directory[256];
      char UserID[12];
      char Password[8];
      char LogonDate[8];
} BVLogonRecord;

The Visual Basic examples show the following:
    Type BVLogonRecord
        Directory               As String * 256
        UserID                  As String * 12
        Password                As String * 8
        LogonDate               As String * 8
    End Type

    Declare Function BVLogonWithDir Lib "bvapi.dll" (LogonRecord As BVLogonRecord) As Integer

VB function:
    Dim LogonRecord As BVLogonRecord

    LogonRecord.Directory = "C:\DBDIR"
    LogonRecord.UserID = "USER"
    LogonRecord.Password = "PASSWORD"
    LogonRecord.LogonDate = "20041117"

    result = BVLogonWithDir(LogonRecord)

So, I tried this in C#:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
 public struct BVLogonRecord
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
   public String Directory;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=12)]
   public String UserID;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
   public String Password;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
   public String LogonDate;

[DllImport(@"C:\BV\bvapi.dll", CharSet=CharSet.Ansi)]
public static extern int BVLogonWithDir ([MarshalAs(UnmanagedType.Struct)] ref BVLogonRecord LogonRecord);

public void Logon()
  string dir = @"C:\DBDIR".PadRight(256,' ');
  string usr = "USER".PadRight(12,' ');
  string pwd = "PASSWORD".PadRight(8,' ');
  string dt = "20041117".PadRight(8,' ');

  BVLogonRecord lr = new BVLogonRecord(dir,usr,pwd,dt);

  int result = BVTest.BVLogonWithDir(ref lr);

I have been able to come up with two results:
1)  if I append all the strings together and store them in Directory, then the function works. (regardless of what's stored in the other variables)
2)  everything else I do gives me bad parameter errors, missing paramters,

Thanks very much for any assistance,
Trevor Braun

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try like this,

public void Logon()
  BVLogonRecord lr;
  lr.Directory = @"C:\DBDIR";
  lr.UserID = "USER";
  lr.Password = "PASSWORD";
  lr.LogonDate= "20041117";
  int result = BVTest.BVLogonWithDir(ref lr);
 string dir = @"C:\DBDIR".PadRight(256,' ');
  string usr = "USER".PadRight(12,' ');
  string pwd = "PASSWORD".PadRight(8,' ');
  string dt = "20041117".PadRight(8,' ');

I think these strings are longer than required by function. With NULL chatacter in the end dir has 257 length.
You can remove PadRight, it is not necessary.

 string dir = @"C:\DBDIR";
Hi TrevStar22,

Please make your commect here. Don't post them as feedback. I think you are new to EE. Read help for more ins...

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

TrevStar22Author Commented:
Thanks Chester.  Yes, I am new to EE.  I'm reading the help now!!  

TrevStar22Author Commented:
To respond to Chester and Alex properly (i.e. via comments):

Adding 'ref' to the structure got rid of the "object...reference" error.  
I think I am no trying to understand how to change the VB syntax "String    *   256"  to something in C#/.Net.

You are correct about the PadRight.  It isn't needed, however I like being 100% of what's being sent to the DLL.  I checked the length of the structure, it is 284 (as expected), however I don't know if that's due to the SizeConst instructions on each property or not.

Thanks very much for the suggestings.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.