Interfacing MS Access with an external DLL/COM object created in C#

Andy Brown
Andy Brown used Ask the Experts™
on
I have an MS Access database and need to interface with a C# DLL that is about to be created for me (as a COM object).  My C# guy has no experience in interfacing with MS Access, and I have only interfaced Access with COM objects that I have purchased (EASendMail etc).  

We have run a few tests, and I can half interface with his test DLL (once referenced), but the Intelisense isn't working correctly.  However, whilst I am sure that we can overcome that one easily enough, it's the return messages that I am struggling to get my head around.  How do I tell Access to listen for them and then, trigger an event/function?

I know this is a little vague - but I could really do with some pointers to get started.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
If you have managed to reference it and instatiate it then its working on the Access side...the problems probably are due to the .Dll itself.
If you give a rough outline of what you want to accomplish along with some sample code it would be easier to get a useful answer.
Andy BrownDeveloper

Author

Commented:
Thanks John.  I agree that the reference issue will be the DLL side of things (as mentioned the c# developer is great in certain areas - but this is new to him). However, the big issue is how the DLL pushes information back into Access so that Access can do something.  So, I guess we're looking for a guide/best-practices approach for .Net.

Initially, I wanted us to create a function that allowed users to press a 'Call' button on the Access DB and then monitor for the connection/returned signals being received.  We have already created a service that can initiate the call from a command line prompt and then I get Access to periodically check an XML file, which has the end-of-call status.  This does work - but, it's not slick and I want to be able to do more than just Call a number, so getting a way to send/receive signals cleanly is essential.
John TsioumprisSoftware & Systems Engineer

Commented:
The Dll should return an object...then you should cast the object to the type you want it..
Put a breakpoint and check what you get in the Watch window
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Andy BrownDeveloper

Author

Commented:
Thanks again John - I understand what you are saying - but we can't seem to get that far.  Do you know of any simplistic examples VBA/.NET/VBA?
John TsioumprisSoftware & Systems Engineer

Commented:
Check this example here
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
How do I tell Access to listen for them and then, trigger an event/function?

 The answer is you don't.   Just like calling a function in VBA, you would get a return value.  ie.

   intRet = SomeFunction()

 It's not class based processing where you'd create an object based on a class, and then potentially hook that into events with "With Events".    It's more like calling something in the Win API.


Jim.
Senior .Net Developer
Commented:
This works for C# 4.0 and Access 2003.  

Tell your C# developer to do the following:

1)
Add this to the top of the class.
  [ClassInterface(ClassInterfaceType.AutoDual)]

Open in new window


2)  On the property of the project (NOT the solution)  make sure the checkbox is checked for MAKE Assembly COM-Visible.  This must be done on every project.

3)  When you receive the DLL copy it to C:\Windows\SysWow64\

Then run the following:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe "c:\windows\syswow64\YOURDLL.dll"  /register /codebase /tlb:"c:\windows\syswow64\YOURDLL.tlb"

Open in new window


You should be adding a reference to the TLB.

(To update the DLL UNREGISTER C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /unregister  "c:\windows\syswow64\YOURDLL.dll"  )

4)  No class may be a static class.   If you have static classes that you're trying to invoke I've run into issues with them.  To get around that, I create a wrapper class and then just invoke the static API, exposing the same parameters as needed.


That should just about do it.
Andy BrownDeveloper

Author

Commented:
Thank you, everyone - sorry for the delay in coming back to you.  That should give us enough information to move forward.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial