|
|
Comments and Discussions
|
|
 |

|
hello,
I'm searching for a way to call a function that i load dynamicly.
In my DLL are the functions CreateForms();ShowForms();CloseForms();
It's should be like I load ones my dll and afther that I call functies from that
DLL from a button. But how can you to that??
|
|
|
|

|
Hi I am trying to build your project but am having issues linking Invoke.obj. When I try to execute
D:\Documents and Settings\208047355.GESMAM\My Documents\codeproject\loadlibrary\
article_src>link /DLL /entry:DLLMain /machine:i386 /subsystem:windows /out:Invok
e.dll /export:InvokeFunc Invoke.obj
it is returning the following error:
Microsoft (R) Incremental Linker Version 7.10.6030
Copyright (C) Microsoft Corporation. All rights reserved.
Creating library Invoke.lib and object Invoke.exp
Invoke.dll : warning LNK4086: entrypoint '_DLLMain' is not __stdcall with 12 byt
es of arguments; image may not run
LINK : error LNK2001: unresolved external symbol _DLLMain
Invoke.dll : fatal error LNK1120: 1 unresolved externals
Any idea how I fix this?
Thanks
Nate
|
|
|
|

|
I have a DLL in C++ and I want to use a function but in an application in C#.
The header of the function in C++ is:
__declspec(dllexport) short DLGet(LPSTR VarName, LPVOID Destination, LPSTR Format)
The second parameter is returned from the function.
What I want to know is How to pass those parameters to that function in my C# application?
I would be very appreciated if someone could help.
Thank you
GCLopes
|
|
|
|

|
How to compile code written in assembler language?
|
|
|
|

|
I am invoking a function from a 3'rd party dll. It works fine by using this method you 've mentioned in your article on XP. Also on Vista if UAC is disabled it works fine too. But if UAC is turned on method cannot work and returns an error code. How can we give appropriate rights do that InvokeFunc which is dynamically loaded into memory?
|
|
|
|

|
Can I make an executable with a particular dll including in it?
I mean linking a dll at compile time only. So that I can run that exe from anywhere without having dll anywhere.
|
|
|
|

|
Hi,
See the subject.
Best regards, Eugen.
|
|
|
|

|
Hi,
This seems to be the solution to my problem!
I have a dll that was wrote in C++ and I need to call that dll and get the return values
Is it possible to get the return values?
How?
Maybe your code is already doing this but I didn't understood your code very well so I would like this explanation if you don't mind.
Kind Regards
P.S.: If this works you've done a great job... Very nice indeed
|
|
|
|

|
I want to add DLL dynamically.and want to invoke the method of DLL.I got the information that one Loadlibrary() funcion is there in c#.net.I tried that but it is not working.
my requirement to add add dll in form code diring form load event.
please tell me the code.
and also specify that on which object I should call that Loadlibrary().and code to access the funcions.
plz reply me.its argent..
Prachi
|
|
|
|

|
Hi,
Is it possible to use DllImport without setting the extern to be static?
Can I use an unmanged code DLL as a non-static object?
Thanks in advance,
Val
|
|
|
|

|
Sir,
Can you please give information related to use of .Net dll in VC++ or Borland C++.
Because I have a .Dll which is developed in .Net Frame work
And I want to use it in Borland C++ .
So I request to give information reklated to How camn i use it.Is it requred any dependancy or not ?
Thank You.
Jagdish
|
|
|
|

|
You can execute procedures inside of DLL's using the CallWindowProc function.
[DllImport("user32", EntryPoint="CallWindowProc")]
public static extern int CallWindowProcA(int lpPrevWndFunc, int hwnd, int MSG, int wParam, int lParam);
Call it with the following, passing in the address of the function you got with GetProcAddress.
CallWindowProcA(funcaddr, 0, 0, 0, 0);
All from within the C# environment.
Scott Carr
OpenOffice.org
Documentation Maintainer
http://documentation.openoffice.org
|
|
|
|

|
Hi,
executing the provided makefile only works within the command promt of the Visual Studio .NET 2003, which has the appropriate environment.
Is there a possibility using the makefile from the 'normal' command prompt?
I set the environment for using MS Visual C++ tools via vcvars32.bat, but it seemed that it isn't enough. If i execute the makefile with
nmake /all
i got the following message:
'ml' is not recognized as an internal or external command, operable program or batch file.
NMAKE : fatal error U1077: 'ml' : return code '0x1'
Stop.
Any ideas?
Kind regards, daniele.
|
|
|
|

|
Hi
i want to get back from my DLL function1 a pointer to an object.
then - i want to activate a method of that object.
can i do it with C# in this way?
in C++ i had a struct of two virtual functions.
my first func - function1, gave me the pointer to the object from type myStruct. then i activatd the functions inside of it.
|
|
|
|

|
Hi,
I am new to Win32 and I found a possible solution. I am wondering if it is valid though since I might not completely understand the problem.
Anyway, Navigate to the Url below and scroll down to the very bottom heading "Loading from Different Locations" (then read the 2 paragraphs)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp[^]
Basically it says you can call LoadLibrary to define the path of your .dll.
ie.
int hmod=LoadLibrary("C:\\PwApi.dll");
then in your dllImport where .net sees [DllImport("PwApi.dll")] it will refer to the .dll that was called by LoadLibrary.
It solves the dynamic path issue I think.
Here is some example code: (the .dll is 3rd party so you can't really test it but you can read the code to get an idea what I am trying to say.)
=================
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace XXX.XXXXXX.XXXXX
{
///
/// This parser class is a wrapper for the 3rd Party
/// vendor balh blah blah's library....
///
public class Parser
{
[DllImport("PwApi.dll")]
public static extern int PwInitParsers(string dictionaryPath);
#region Address Functions
[DllImport("PwApi.dll")]
public static extern int PwParseAddr(string sourceAddress);
[DllImport("PwApi.dll")]
public static extern int PwFraction(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwHouseNumber(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwHousePrefix(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwHouseSuffix(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwPostDir(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwBox(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwPreDir(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwRoute(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwStreetName(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwStreetSuffix(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwUnitValue(byte[] target);
[DllImport("PwApi.dll")]
public static extern int PwUnitDesignator(byte[] target);
#endregion
[DllImport("kernel32")]
public extern static int LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
public extern static bool FreeLibrary(int hLibModule);
[DllImport("kernel32", CharSet=CharSet.Ansi)]
public Parser()
{
}
public Address ParseAddress(string addressToParse)
{
int returnCode;
int hmod;
try
{
hmod=LoadLibrary("C:\\PwApi.dll");
string dictionaryPath = @"C:\Default_100.dct"; // AppDomain.CurrentDomain.BaseDirectory + "Default_100.dct";
returnCode = PwInitParsers(dictionaryPath);
if(returnCode != 0)
{
string msg = String.Format("An error occurred loading the dictionary for PwApi.dll. Please ensure the dictionary path is correct: -->{0}<-- Error Code: -->{1}<--",dictionaryPath, returnCode);
throw new Exception(msg);
}
}
catch(Exception ex)
{
throw new Exception("There has been an error Initializing the PwApi.dll. Description:" + ex.Message);
}
Address address = new Address();
byte[] target;
if(PwParseAddr(addressToParse)==0) //If not successful leave everything blank.
{
target = new byte[250];
returnCode = PwFraction(target);
address.Fraction = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwHouseNumber(target);
address.HouseNumber = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwHousePrefix(target);
address.HousePrefix = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwHouseSuffix(target);
address.HouseSuffix = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwPostDir(target);
address.PostDirection = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwBox(target);
address.PostOfficeBox = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwPreDir(target);
address.PreDirection = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwRoute(target);
address.Route = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwStreetName(target);
address.StreetName = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwStreetSuffix(target);
address.StreetSuffix = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwUnitValue(target);
address.Unit = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
target = new byte[250];
returnCode = PwUnitDesignator(target);
address.UnitDesignator = ASCIIEncoding.ASCII.GetString(target,0,returnCode);
}
FreeLibrary(hmod);
return address;
}
}
}
Trevor
|
|
|
|

|
Hello
I use the invoke dll in windows applications and it works fine.
I used the same .Net Wrapper (VB), in a .Net WebService and I have a probleme because my wrapper doesn't find the dll I load dynamicaly. (LoadLibrary return 0)
I know that Asp.Net make a copy of binary folder in cache.
How can I resolve this probleme
Thanks in advance
Sébastien
|
|
|
|

|
Hi
I have " int func(char* names[]) " function.
I need to call thus C++ dll function and get an array of names.
How I need to write my c# func?
Thanks
koby
|
|
|
|

|
Here is the progress so far, but I am not able to successfully get a valid file handle to the window and show it. It does not crash however. This will aid to your progress as well.
namespace MyApp
{
struct WNDCLASSEX
{
public uint cbSize;
public uint style;
public long lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public long hInstance;
public long hIcon;
public long hCursor;
public long hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public long hIconSm;
}
class Class1
{
public const string KERNEL32_DLL = "kernel32.dll";
[DllImport(KERNEL32_DLL, EntryPoint="GetModuleHandle")]
static extern int GetModuleHandleA(string lpModuleName);
public const string USER32_DLL = "user32";
public const uint CS_VREDRAW = 0x0001;
public const uint CS_HREDRAW = 0x0002;
public const long WS_OVERLAPPED = 0x00000000L;
public const long WS_EX_APPWINDOW = 0x00040000L;
public const long WS_SYSMENU = 0x00080000L;
public const int SW_SHOWNORMAL = 1;
[DllImport(USER32_DLL, EntryPoint="MessageBox")]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);
[DllImport(USER32_DLL, EntryPoint="GetForegroundWindow")]
public static extern int GetForegroundWindow();
[DllImport(USER32_DLL, EntryPoint="RegisterClassEx")]
public static extern short RegisterClassExA(ref WNDCLASSEX pcWndClassEx);
[DllImport(USER32_DLL, EntryPoint="CreateWindowEx")]
public static extern long CreateWindowExA(long dwExStyle, string lpClassName, string lpWindowName, long dwStyle, long x, long y, long nWidth, long nHeight, long hwndParent, long hMenu, long hInstance, object lpParam);
[DllImport(USER32_DLL, EntryPoint="ShowWindow")]
public static extern int ShowWindow(long hwnd, int nCmdShow);
[DllImport(USER32_DLL, EntryPoint="UpdateWindow")]
public static extern int UpdateWindow(long hwnd);
[DllImport(USER32_DLL, EntryPoint="SetFocus")]
public static extern int SetFocus(long hwnd);
[DllImport(USER32_DLL, EntryPoint="ShowCursor")]
public static extern int ShowCursor(bool bShow);
[DllImport(USER32_DLL, EntryPoint="UnregisterClass")]
public static extern int UnregisterClassA(string lpClassName, long hInstance);
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
WNDCLASSEX wndclassex = new WNDCLASSEX();
wndclassex.cbSize = 48;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = 0;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hIcon = 0;
wndclassex.hCursor = 0;
wndclassex.hbrBackground = 0;
wndclassex.lpszMenuName = "";
wndclassex.lpszClassName = "myclassname";
wndclassex.hIconSm = 0;
wndclassex.hInstance = 0;
RegisterClassExA(ref wndclassex);
long hWnd = CreateWindowExA(
WS_EX_APPWINDOW,
wndclassex.lpszClassName,
"TAGML",
WS_OVERLAPPED | WS_SYSMENU, // Window won't be resizable
0,
0,
300,
300,
0,
0,
wndclassex.hInstance,
0);
if(hWnd == 0)
{
MessageBox(0, "Unable to create window", ".NET", 0);
UnregisterClassA(wndclassex.lpszClassName, wndclassex.hInstance);
return;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
SetFocus(hWnd);
ShowCursor(true);
//UnregisterClassA(wndclassex.lpszClassName, wndclassex.hInstance);
|
|
|
|

|
.Net v2.0 has no need for the InvokeFunc thunk. Instead, the System.Runtime.InteropServices.Marshal class has a GetDelegateForFunctionPointer() method which obtains a delegate for any unmanaged function pointer.
|
|
|
|

|
Okay, So You declare:
[DllImport("Invoke", CharSet=CharSet.Unicode)]
public extern static int InvokeFunc(int funcptr, int hwnd,
string message, string title, int flags);
So if I want to use Invoke.dll to call a different function WHICH has a different signature - maybe taking two ints and a Guid,I would have to declare the following:
[DllImport("Invoke", CharSet=CharSet.Unicode)]
public extern static int InvokeFunc(int funcptr, int par1, int par2, Guid par3);
is that correct?
|
|
|
|

|
Hello, i need to create a similar Dll to handle the value of the GetProcAddress() function. The value returned by GetProcAddress() is an int value, and i guess i'd need a pointer to this int to be able to call a specific function of an API : WinStationQueryInformationW from WinSta.dll
The problem is i'm bad at C++ so i can't create a dll similar to your Invoke.dll nor understand what you've done in it. :-/
Is it possible to handle the invoking part within a c# class ?
Cause your sample code enables me to use loadlibrary + getProcAddress and make it works with your invokefunc but it's not suitable to call WinStationQueryInformationW.
I'm sorry i'm french and not very experienced so my arguments must be kind of difficult to understand. Anyway, i would be very happy if you can help me.
Thanks
|
|
|
|

|
Richard,
Nice article!
Another .NET/DLL related question.
How do I implement/simulate DllMain in C#?
As you can imagine the most intriguing part of this question would be the DLL_PROCESS_DETACH event -- Say I want to do some DLL (sorry, assembly) specific cleanup. (Doesn’t it sound similar to the destructor related “issues”?
Thanks in advance.
Alex
|
|
|
|

|
Thanks for the article,
Please provide guidelines to make the EXE using VS.Net...
How to link the .asm and .obj files in the IDE?
Thx,
UK
|
|
|
|

|
I downloaded the source code that has been posted here and tried to compile and run it as-is with Visual Studio .NET. However, I get a System.DllNotFoundException on the following line in the .cs file:
int result=InvokeFunc(funcaddr, 0, "Hello World", ".Net dynamic export invocation", 1 /*MB_OKCANCEL*/);
Why doesn't it like this? "DllNotFoundException" suggests to me that for some reason it's not finding User32.dll; however, this file is on my system. Any suggestions?
|
|
|
|

|
http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp
and
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=7a1fc856-b9ec-41b6-921a-eee7555844de
|
|
|
|

|
How do I complie the Invoke.asm?
I has been try to compile using The nasm, but I found just only following errors everytime
:
invoke.asm:11: attempt to define a local label before any non-local labels
invoke.asm:12: parser: instruction expected
invoke.asm:12: attempt to define a local label before any non-local labels
invoke.asm:14: parser: instruction expected
invoke.asm:15: parser: instruction expected
invoke.asm:15: symbol `option' redefined
invoke.asm:16: parser: instruction expected
invoke.asm:16: symbol `option' redefined
invoke.asm:20: expression syntax error
invoke.asm:21: parser: instruction expected
invoke.asm:24: parser: instruction expected
invoke.asm:24: symbol `DllMain' redefined
invoke.asm:27: expression syntax error
invoke.asm:28: parser: instruction expected
invoke.asm:34: parser: instruction expected
invoke.asm:34: symbol `InvokeFunc' redefined
Please, Would you let me know that method to compile Invoke.asm?
|
|
|
|

|
Hello, this article looks like it may fix an issue I am having with the GetProcAddress. I want to be able to Register an ActiveX DLL or OCX but (like you said) I can do nothing with the Address I get to "DllRegisterServer".
Can you help me with an example to Register a DLL? Greatly appreciate it!
|
|
|
|

|
Hello,
I have a C++-DLL (VC 6.0) which exports some classes.
I want to use this classes and their methods within my C# project.
Problem :
1) I must be able to import a class from a c++ dll
2) exported methods of the C++-Dll take objects as parameter.
Example (C++)
public class X;
public class z;
public class Y
{
X x_object;
Z someMethod(X x_param);
}
How can I use
X x_object;
Z z_object;
z_object = Y.someMethod(x_object);
within the C#? (I dont want to encapsulate my C++-Classes as COM classes. )
I would be happy to get an answer.
Thanks.
|
|
|
|

|
You can achive the same effect by dynamically building a temp assembly that has the correct pInvoke signature and then calling that. Its certainly a lot slower for the first call anyway but the advantage is that you can do it all in c# if thats somthing you need to do.
|
|
|
|

|
I know how to get a function address, however is there any way to get the address of a global variable inside of a dll. I need to access this variable inside my program, but when I access it, I get a linking error. Does anybody have any ideas on how to do this?
Thanks
|
|
|
|

|
Hey, first of all, thanks for writing this article, but I have problems compiling it with VS.NET final...
I'm getting the following errors:
Invoke.asm(21): Expected class, delegate, enum, interface, or struct
Invoke.asm(1): A namespace does not directly contain members such as fields or methods
Invoke.asm(28): Expected class, delegate, enum, interface, or struct
when trying to compile...
could you please tell me what to do? thanks
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Delaying which DLL export to call until runtime is not possible with C#. This article shows you how to make it possible.
| Type | Article |
| Licence | BSD |
| First Posted | 12 Nov 2001 |
| Views | 448,950 |
| Bookmarked | 87 times |
|
|