|
|
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
|
|
|
|
|
Please take a look at GetDelegateForFunctionPointer in the System.Runtime.InteropServices namespace (.Net 2.0)
SDX2000
|
|
|
|
|
|
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
|
|
|
|
|
using System;<br />
using System.Runtime.InteropServices;<br />
<br />
namespace DCC.Install<br />
{<br />
public class ExecuteDLLFunction<br />
{<br />
[DllImport("kernel32")]<br />
public extern static int LoadLibrary(string lpLibFileName);<br />
[DllImport("kernel32")]<br />
public extern static bool FreeLibrary(int hLibModule);<br />
[DllImport("kernel32", CharSet=CharSet.Ansi)]<br />
public extern static int GetProcAddress(int hModule, string lpProcName);<br />
<br />
[DllImport("user32", EntryPoint="CallWindowProc")] <br />
public static extern int CallWindowProcA(int lpPrevWndFunc, int hwnd, int MSG, int wParam, int lParam);<br />
<br />
private string _dllfile;<br />
private int _dll;<br />
<br />
public string dllfile <br />
{<br />
get { return _dllfile; }<br />
set { _dllfile = value; }<br />
}<br />
<br />
public ExecuteDLLFunction(string DLLFile)<br />
{<br />
_dllfile = DLLFile;<br />
_dll = LoadLibrary(_dllfile);<br />
}<br />
<br />
~ExecuteDLLFunction()<br />
{<br />
FreeLibrary(_dll);<br />
}<br />
<br />
public int Execute(string strFunc)<br />
{<br />
int funcaddr;<br />
<br />
if (_dll != 0)<br />
{<br />
<br />
funcaddr = GetProcAddress(_dll, strFunc);<br />
<br />
if (funcaddr != 0)<br />
{<br />
return CallWindowProcA(funcaddr, 0, 0, 0, 0);<br />
} <br />
else <br />
{<br />
return -2;<br />
}<br />
}<br />
else <br />
{<br />
return -1;<br />
}<br />
}<br />
}<br />
}
|
|
|
|
|
Hi,
Great comment and great code! I have some follow-upers that I need some tech savvy people like you to straighten out for me:
Please, if possible, respond quickly as there is a short deadline in my project related to the issues below!
Q1: I have a C++ unmanaged API (binaries only: .libs and .dlls) and I wish to program against it using C# .NET. The API is huge with thousands of functions and classes. Honestly now, is there no way I can, at design time, simply call a function as described in the API's documentation?
Sure, if I do
DllImport("HugeAPI.dll")
public static extern void Test(int x);
all is fine because the return type and the parameter doesn't cause any problems. But the API uses user defined types that I don't have access to, which makes it hard to know what to do in this case:
DllImport("HugeAPI.dll")
public static extern SpecialAPIType Test(int x, AnotherSpecialAPIType type);
Q2: The above only applies to functions/methods in the API, but what about classes and arbitrary types?
For instance, if I want to derive a C# class from a particular C++ API class, what do I do?
Q3: Is there no hope? And no, I can't convert the API using Managed Extensions for C++ and no, I can't wrap the API in a COM server accessing it through COM Interop and no, I won't ever rewrite the entire API in .NET, it will simply take a few years.
Sincerely,
/Tommy
|
|
|
|
|
Glad you enjoyed it. I was having a problem instantiating the DllRegisterServer for registering a dll.
I found the following on using Unmanaged Classes in C#:
http://www.codeguru.com/Cpp/Cpp/cpp_managed/interop/article.php/c6867/
Not sure if it'll help. I havn't had to write against Unmanaged C++ myself.
Scott Carr
OpenOffice.org
Documentation Maintainer
http://documentation.openoffice.org
|
|
|
|
|
|
how to pass parameters?
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com
|
|
|
|
|
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
|
|
|
|
|
if u use the absolut DLL path.
such as : c:\\xxpath\\xx.dll
it should be work.
joylite
|
|
|
|
|
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);
|
|
|
|
|
Ok at this point I have the callback coming back from the window into C#, but I can't get a valid handle. It keeps coming up as zero. Any idea why the HWND is always zero????
using System;
using System.Runtime.InteropServices;
namespace MyApp
{
public delegate UIntPtr FPtr(IntPtr hWnd, uint uMsg, UIntPtr wParam, UIntPtr lParam);
struct WNDCLASSEX
{
public UInt32 cbSize;
public UInt32 style;
public FPtr lpfnWndProc;
public Int32 cbClsExtra;
public Int32 cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
///
/// Summary description for Class1.
///
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 UInt32 CS_VREDRAW = 0x0001;
public const UInt32 CS_HREDRAW = 0x0002;
public const UInt32 WS_EX_APPWINDOW = 0x00040000;
public const UInt32 WS_OVERLAPPED = 0x00000000;
public const UInt32 WS_SYSMENU = 0x00080000;
public const Int32 SW_SHOWNORMAL = 1;
public const int WM_QUIT = 0x0012;
[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 IntPtr CreateWindowExA(UInt32 dwExStyle, string lpClassName, string lpWindowName, UInt32 dwStyle, Int32 x, Int32 y, Int32 nWidth, Int32 nHeight, IntPtr hwndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport(USER32_DLL, EntryPoint="FindWindow")]
public static extern IntPtr FindWindowA(string lpClassName, string lpWindowName);
[DllImport(USER32_DLL, EntryPoint="FindWindowEx")]
public static extern IntPtr FindWindowExA(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport(USER32_DLL, EntryPoint="ShowWindow")]
public static extern int ShowWindow(long hwnd, Int32 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, IntPtr hInstance);
public static UIntPtr WinProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, UIntPtr lParam)
{
Console.WriteLine("WinProc Callback called!!!!!!!");
return UIntPtr.Zero;
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
string ClassName = "MyClassName";
string WindowName = "TagML";
WNDCLASSEX wndclassex = new WNDCLASSEX();
wndclassex.cbSize = 48;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = new FPtr(WinProc);
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = IntPtr.Zero;
wndclassex.hIcon = IntPtr.Zero;
wndclassex.hCursor = IntPtr.Zero;
wndclassex.hbrBackground = IntPtr.Zero;
wndclassex.lpszMenuName = "";
wndclassex.lpszClassName = ClassName;
wndclassex.hIconSm = IntPtr.Zero;
RegisterClassExA(ref wndclassex);
IntPtr hWnd = CreateWindowExA(
WS_EX_APPWINDOW,
wndclassex.lpszClassName,
WindowName,
WS_OVERLAPPED | WS_SYSMENU, // Window won't be resizable
0,
0,
300,
300,
IntPtr.Zero,
IntPtr.Zero,
wndclassex.hInstance,
IntPtr.Zero);
hWnd = FindWindowA(wndclassex.lpszClassName, WindowName);
hWnd = FindWindowExA(IntPtr.Zero, IntPtr.Zero, wndclassex.lpszClassName, WindowName);
if(hWnd == IntPtr.Zero)
{
MessageBox(0, "Unable to create window", ".NET", 0);
UnregisterClassA(wndclassex.lpszClassName, wndclassex.hInstance);
return;
}
|
|
|
|
|
Why isn't CreateWindowEx returning valid window handle?
Easy to check Source Files:
http://www.tagenigma.com/qa/CSharp.Win32.zip
Do I need to setup a hook or something?
http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/
|
|
|
|
|
Declare a wndProc function . Dont set it to 0;
Ravindra Kumar Sharma
SE ,Monitors
|
|
|
|
|
.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.
|
|
|
|
|