LoadLibrary Help

J

Jake Thompson

Does anybody know the answer to the following issue

I am using the standard LoadLibary function fromn MSN

It is called from a "C" exe to load up a C++ DLL

The C++ DLL has an import statement to create a pointer to a VB.net
com interopt program.

I am using the TLB instead of the DLL. In the past (which always
worked) I used the DLL extention but the program the dll interfaced
with was written in standard COM for VB.6 and not .net.

Now that the interface program is writen in .net com interopt my
loadlibrary call to my DLL will not respond. No timeout, no error just
hangs.

Is this a unmanaged to managed thing, a compiler setting issue, or
just can't get there from here using LoadLibrary.

I am open to ideas but note that the main legacy system is written in
"C" and the easist way at the to access the original VB program was
through Globalprocs and smartpointers.

UIltimately what I would like to do is skip the C++ and go straight to
the .net module from "C" - grab my data and bring it back to the
legacy program but again that would be much more difficult from my
understanding. I don't have a problem reworking the C++ program but is
that really my problem? It seems that the real problem is the
LoadLibrary call and that it just doesnt like the TLB file even though
the C++ program will find the functions and compile correctly.

So I hope someone can offer advice as to what options I have or what I
can try to get around this mess.

Any ideas?

Thanks
 
J

Jake Thompson

You're really looking for help in the wrong place, this is a Win32 API
question, not a C question. Anyway, from what I recall, you can't call
managed code from unmanaged code, the other way around should work though
using P/Invoke.

--
--------------------------------------------------------------
Professional mobile software development
BreezySoft Limitedwww.breezysoft.com
--------------------------------------------------------------- Hide quoted text -

- Show quoted text -

Thanks for the Reply Lionel but since it was dealing with so many
different languages and since LoadLibary is used in C I thought I
would star there. Anyway you can call managed code from unmanaged
code - I found by experimenting that it is possible to do what I want)
- Overall I think it was putting the functions within an Interface of
declaration that solved the issue but it could also be that the key
was strongly tied to the compiled .net dll - Anyway here is my example
of a C# or VB.net dll being called from C++ dll that is called from a
C module that uses LoadLibary and GetProc calls

Example of managed code that you want to call in C#

// Interface declaration.
public interface IFoo
{
int TestFoo1(int NumValue)
};

public class FooTest: IFoo
{
public int TestFoo1(int NumValue)
{
If NumValue = 7
{
Return 0
}
Else
{
Return 1
}
}
}

Example of managed code that you want to call in VB

Public Interface IFoo
Function TestFoo1(ByVal NumValue As Integer) As Integer
End Interface

Public Class FooTest
Implements IFoo
Public Function TestFoo1(ByVal NumValue As Integer) As Integer
Implements IFoo.TestFoo
If NumValue = 7 Then
Return 0
Else
Return 1

End If

End Function

Then

2) Generate a snk file and refer to AssemblyInfo.CS or VB file for
the project

from the Vs2008 cmd prompt - type

"sn.exe -k VbtestFoo.snk"

This creates the VbtestFoo.snk in vs9.0 directory which should be
moved to the location of your AssemblyKeyFile directory

3) Put this in your AssemblyInfo.CS or VB
For C#
[assembly: ComVisible(true)]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("..\\..\\VbtestFoo.SNK")]
For VB
<Assembly: ComVisible(True)>
<assembly: AssemblyDelaySign(false)>
<Assembly: AssemblyKeyFile("C:\foo\webtest\keys\VbtestFoo.snk")>
4) Compile

5) Do a Regasm

Regasm.exe VbtestFoo.dll /tlb:VbtestFoo.tlb/ codebase



part 2 - 4 steps the unmanaged side c++

1) In the c++ dll put two lines of code at the top of the module that
calls the .net file

#import "C:\foo\VBtestFoo.tlb" raw_interfaces_only <----referring to
the tlb not the standard dll used in pre .net
using namespace VBtestFoo;

2) In the function that calls a member in the .net dll put the
following

IFooPtr ptrFooTest(__uuidof(FooTest)); <--- the instantiaion of the
ptr to the class

note* Don't know why but C++ does not like that instantiation of the
ptr as a global value for the .net stuff that is why it's instaniated
at the function level - This was never a problem for calls to non .net
dll's

ptrFooTest->TestFoo1(thenum, &thenum2); <---- Function in .net dll

3) Put C++ dll in executing directory of the legacy C code and refer
to it using LoadLibary and Getproc will run sucessfully

4) Done


Hope this helps those who have had or will have similar issues - Again
most likely the wrong group but at least there is a solution that can
be found somewhere when there is so much overlap of different
languages

Jake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top