Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# dll in VFP

Status
Not open for further replies.

crisboy

Technical User
Sep 10, 2010
5
HU
Hi All!

I'm new in here. I have a DLL (this is a COM object), this developped in C# .NET 2.0. I want to use this DLL in VFP. How can I do that?
I try to use it, but I have got an error message "Cannot find entry point ..." This DLL is work fine in Delphi.

Thanks
Cris
 
Chris,

Welcome to the forum.

If you're sure that the DLL is a COM file, you might first need to register it on your computer (and your end-users'). You do that by running REGSVR32.EXE, passing the full path and filename of the DLL.

You will then need to instantiate it within VFP, using the CREATEOBJECT() function. For that, you will need to know the COM class name. The person who wrote the DLL can tell you that.

After that, you can access the properties and methods of the class. Again, you will need to consult the person who wrote it to find out what those are.

If the DLL is something other than a COM object, then you above does not apply.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thank's Mike the answer!

Yes, this is a COM object, and my collegue wrote this. This is a sample. He make a reg file for this DLL, and I registering with this.Need registering a regsvr32 too?

Here is the C# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace probadll
{
public interface IProbaDll
{
int Add(int a, int b);
string HelloWorld();
}
[ClassInterface(ClassInterfaceType.None)]
public class Class1:IProbaDll
{
public int Add(int a, int b)
{
return a + b;
}

public string HelloWorld()
{
return "Hello world";
}
}
}

And the VFP:
DECLARE STRING Class1.HelloWorld IN probadll.dll AS dll_fv
?dll_fv()

I don't know where is the problem.

Chris
 
REGSVR32 won't work with a .NET COM DLL. Use RegAsm instead.

But, you're doing the wrong thing. DECLARE only works for Win32 DLLs. For COM, you need to use CREATEOBJECT().

Craig Berntson
MCSD, Visual FoxPro MVP,
 
Chris,

If you have a .REG file, you don't need to run REGSVR32. Running the .REG file does the same thing. The only reason many developers prefer REGSVR32 is that it can be run from a command-line, and can therefore easily be added to a setup routine.

Craig is right when he said REGSVR32 won't work with a .NET DLL, which is why I specifically asked about it being a COM object.

He's also right when he said that you don't want to use DECLARE. That's for an entirely different kind of DLL.

I'm guessing from your C# code that the class name is Class1:IProbaDll. If so, the VFP code will be something like this:

Code:
loDLL = CREATEOBJECT("Class1:IProbaDll")
lcMessage = loDLL.HelloWorld()
MESSAGEBOX(lcMessage)  && Displays Hello World
lnSum = loDLL.Add(10, 15)
MESSAGEBOX(lnSum)      && Displays 25

If you run that in the VFP command window, one line at a time, Intellisense will pop up at various points, and that will show you the properties and methods that are exposed by the class.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
For C# assembly DLLs COM enabled via interop your collegue could have used regasm with the command switch /regfile to create the .reg file Mike speaks of. But havin the dll and no such file you can also simply execute regasm with the dll name like you normally do with regsvr32.

Bye, Olaf.
 
crisboy,
1) To make your class visible use ClassInterfaceType.Autodual and also ComVisibl true.
2) Be sure that you target x86 otherwise your DLL would fail with VFP on 64 bits OS.
3) Use RegASM to register.
4) Activex DLLs are COM Dlls and not instantiated using Declare as you do with win32 dlls.
5) I suggest including a ProgID attribute.

Here is a sample using your code:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace probadll
{    
	[InterfaceType(ComInterfaceType.InterfaceIsDual)]
	public interface IProbaDll    
	{        
		[DispId(0)]
		int Add(int a, int b);        
		[DispId(1)]
		string HelloWorld();
	}

	[ComSourceInterfaces(typeof(IProbaDll))]
	[ClassInterface(ClassInterfaceType.AutoDual)]
	[ProgId("MyCOM.MyClass")]
	[ComVisible(true)]
	public class Class1:IProbaDll    
	{        
		public int Add(int a, int b)        
		{
			return a + b;        
		}        
		public string HelloWorld()        
		{
			return "Hello world";
		}
	}
}

Create a key file, compile with target x86 and register using regasm:

sn -k mykey.snk

csc /t:library /keyfile:mykey.snk /platform:x86 myTest.cs

regasm myTest.dll /codebase

From VFP you would call it like:

Code:
o = createobject('MyCOM.MyClass')
? o.Add(3,5)
? o.HelloWorld()

You can find a more detailed sample here:

For the best and most complete sample and explanations check here:

Cetin Basoz
MS Foxpro MVP, MCP
 
Hi All!

I'm come back in yesterday.

cbasoz, we try what you wrote, and didn't work. Copy your C# code, and make the key file, compile, and registering.
try to copy the VFP, and we have got an error message "object not found". And really in the OS not found this COM object.
Chris
 
Dear cbasoz!

It's working fine! We not the correct dll registering.

Thanks the answers!

Chris
 
Chris,

You say it's working fine now.

For the benefit of others, could you let us know what exactly you did to get it working. And what the original problem was. Did it turn out to be that the DLL wasn't registered, or was the problem in the code you were using to call it?

As well as helping other people who might have the same issue, it would also be a courtesy to the several people who posted answers to your question.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,

my colleque copying cbasoz's code, but the ProgId's not "MyCOM.MyClass" in this DLL, and I in VFP try creating object with "MyCOM.MyClass".
Cbasoz answer is correct! Thank you for everybody!

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top