473,836 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in using win32 DLL in c#

hi ,everyone ,please check my codes,i am really confused.

the codes of win32 dll is as follows:
*************** ********the win32 DLL writen in c++************ *****
extern "C"
{
__declspec(dlle xport) WCHAR* __stdcall getWNameOfCompo nnet();

}

WCHAR name[100];

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_c all,
LPVOID lpReserved
)
{
return TRUE;
}
WCHAR* getWNameOfCompo nnet(){
return name;
}

*************** *************** *************** ***
*************** ******the win32 .def file*********** *

EXPORT
getWNameOfCompo nnet @1
*************** *************** *************** ********
and I try to use the export in c#,but I can not get anything from
getprocaddress( ,.)

**************i mport the prototype of api**
[DllImport ("kernel32.dll" ,EntryPoint="Lo adLibraryW",
CharSet=CharSet .Unicode,Callin gConvention=Cal lingConvention. StdCall,
SetLastError =true)]
public extern static IntPtr LoadLibrary(Str ing lpFileName);

[DllImport("Kern el32", EntryPoint = "GetProcAddress ",
CharSet=CharSet .Unicode ,CallingConvent ion =
CallingConventi on.StdCall,
SetLastError = true)]
public extern static IntPtr GetProcAddress(
IntPtr handle,
[MarshalAs(Unman agedType.LPWStr )]string funcname);

*************** *******
**************g et the export from my dll***
public delegate string getwname();

private static Delegate GetAddress(IntP tr dllModule, string
functionname, Type t)
{

IntPtr addr = GetProcAddress( dllModule, functionname);
//addr is always zero.why?

if (addr == IntPtr.Zero )
return null;
else
return Marshal.GetDele gateForFunction Pointer(addr, t);
}

public String callGetWname()
{
IntPtr huser32 =IntPtr.Zero ;
huser32 = LoadLibrary("my .dll");//the huser32 is ok,the
dll is loaded.
getwname mygetw = (getwname)GetAd dress(huser32,
"getWNameOfComp onnet",
typeof(getwname ));

}

*************** *************** **
the getProcAddress( ..) doesn't work.Where am i wrong?
Jul 8 '08 #1
8 4345
the getProcAddress( ..) doesn't work.Where am i wrong?

Does the filename of the DLL you need to load change after compile time? If
not, let p/invoke handle all the dynamic loading mess for you and just use
DllImport and declare the function you need to call.
Jul 8 '08 #2
hi,Ben

the export of the DLL is right. I check them with a tool software.
and I change the prototype of getProcAddress( ..) in C# as follows:
*************** ***********
[DllImport("Kern el32.dll", EntryPoint = "GetProcAddress ", CharSet =
CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall , SetLastError =
true)]
public extern static IntPtr GetProcAddress(
IntPtr handle,
[MarshalAs(Unman agedType.LPStr)]string funcname);
*************** ************

if I change it into unicode ,everything go wrong. I don't why ,maybe
GetProcAddress( ) only has ANSI version.

just put result here for someone may encounter the same problem.

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pamдÈëÏûÏ¢ÐÂÎÅ :eC************ **@TK2MSFTNGP06 .phx.gbl...
>the getProcAddress( ..) doesn't work.Where am i wrong?

Does the filename of the DLL you need to load change after compile time?
If not, let p/invoke handle all the dynamic loading mess for you and just
use DllImport and declare the function you need to call.

Jul 9 '08 #3
**************i mport the prototype of api**
[DllImport ("kernel32.dll" ,EntryPoint="Lo adLibraryW",
CharSet=CharSet .Unicode,Callin gConvention=Cal lingConvention. StdCall,
SetLastError =true)]
public extern static IntPtr LoadLibrary(Str ing lpFileName);
Just FYI, you shouldn't specify the LoadLibraryW entry point unless you
specified ExactSpelling = true on the DllImport attribute. You're already
specifying to use the Unicode character set, as such that would be handled
automatically. Also, you don't need to specify the EntryPoint unless it's
different than the name of the function you're defining. Just some nice
things you might want to do to help simplify your code. :o)

For example: This would need it.
[DllImport("myfi le.dll", EntryPoint = "Foo")]
public static extern void Bar();

After looking in both the WinBase.h and Windows.h header files the MSDN
documentation states they're located in there was only a single declaration
for the GetProcAddress function - which leads me to believe you are correct
when you said that only the ANSI version is available. Not to mention there
is no information in the MSDN documentation indicating there is both a
Unicode and ANSI version available.

Perhaps because old C didn't allow Unicode characters in their definitions.
I don't know, I'm completely guessing there. Never used it before so I
couldn't say either way for sure. That's the only reason I could think of
why there wouldn't be a unicode version of the definition.

"Ryanivanka " <sa********@gma il.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
hi,Ben

the export of the DLL is right. I check them with a tool software.
and I change the prototype of getProcAddress( ..) in C# as follows:
*************** ***********
[DllImport("Kern el32.dll", EntryPoint = "GetProcAddress ", CharSet =
CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall , SetLastError
= true)]
public extern static IntPtr GetProcAddress(
IntPtr handle,
[MarshalAs(Unman agedType.LPStr)]string funcname);
*************** ************

if I change it into unicode ,everything go wrong. I don't why ,maybe
GetProcAddress( ) only has ANSI version.

just put result here for someone may encounter the same problem.

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pam>
дÈëÏûÏ¢ÐÂÎÅ:eC **************@ TK2MSFTNGP06.ph x.gbl...
>>the getProcAddress( ..) doesn't work.Where am i wrong?

Does the filename of the DLL you need to load change after compile time?
If not, let p/invoke handle all the dynamic loading mess for you and just
use DllImport and declare the function you need to call.

Jul 9 '08 #4
On Jul 8, 6:34*pm, Ryanivanka <sarah.h...@gma il.comwrote:
the getProcAddress( ..) doesn't work.Where am i wrong?
Here:
[DllImport("Kern el32", EntryPoint = "GetProcAddress ",
CharSet=CharSet .Unicode ,CallingConvent ion =
CallingConventi on.StdCall,
SetLastError = true)]
You either need to use CharSet=CharSet .Auto, or
EntryPoint="Get ProcAddressW". If you specify CharSet explicitly, P/
Invoke won't auto-decorate the function name.
Jul 9 '08 #5
Ryanivanka wrote:
hi,Ben

the export of the DLL is right. I check them with a tool software.
Why are you using GetProcAddress? Use a DllImport attribute on *your*
function setting EntryPoint correctly and p/invoke will call LoadLibrary and
GetProcAddress for you.
>

and I change the prototype of getProcAddress( ..) in C# as follows:
*************** ***********
[DllImport("Kern el32.dll", EntryPoint = "GetProcAddress ",
CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall ,
SetLastError = true)]
public extern static IntPtr GetProcAddress(
IntPtr handle,
[MarshalAs(Unman agedType.LPStr)]string funcname);
*************** ************

if I change it into unicode ,everything go wrong. I don't why ,maybe
GetProcAddress( ) only has ANSI version.

just put result here for someone may encounter the same problem.

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pam>
дÈëÏûÏ¢ÐÂÎÅ:eC **************@ TK2MSFTNGP06.ph x.gbl...
>>the getProcAddress( ..) doesn't work.Where am i wrong?

Does the filename of the DLL you need to load change after compile
time? If not, let p/invoke handle all the dynamic loading mess for
you and just use DllImport and declare the function you need to call.

Jul 9 '08 #6
Thank you, Pavel.
It does work if I kick out the"CharSet" attribute.
-----------------------------------------
ÔÚ Wed, 9 Jul 2008 07:20:02 -0700 (PDT) £¬Pavel MinaevдµÀ£º
>On Jul 8, 6:34*pm, Ryanivanka <sarah.h...@gma il.comwrote:
>the getProcAddress( ..) doesn't work.Where am i wrong?

Here:
>[DllImport("Kern el32", EntryPoint = "GetProcAddress ",
CharSet=CharSe t.Unicode ,CallingConvent ion =
CallingConvent ion.StdCall,
SetLastError = true)]

You either need to use CharSet=CharSet .Auto, or
EntryPoint="Ge tProcAddressW". If you specify CharSet explicitly, P/
Invoke won't auto-decorate the function name.
-----------------------------------------

Ö Àñ£¡
Ryanivanka
sa********@gmai l.com

Jul 10 '08 #7
hi,Ben.
what you ask remind me of another question.

I was told if I use LoadLibrary(..) to load a DLL, I have to freeLibrary(..) by myself too,because CLR won't do that for me? is this correct?

I use LoadLibrary(..) to control the bind of DLL by myself.And if I import my function using EntryPoint,woul d the DLL stay in the process's virtual address space until the process is ended?

-----------------------------------------
ÔÚ Wed, 9 Jul 2008 11:21:38 -0500 £¬Ben Voigt [C++ MVP]дµÀ£º
>Ryanivanka wrote:
>hi,Ben

the export of the DLL is right. I check them with a tool software.

Why are you using GetProcAddress? Use a DllImport attribute on *your*
function setting EntryPoint correctly and p/invoke will call LoadLibrary and
GetProcAddre ss for you.
>>

and I change the prototype of getProcAddress( ..) in C# as follows:
************** ************
[DllImport("Kern el32.dll", EntryPoint = "GetProcAddress ",
CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall ,
SetLastError = true)]
public extern static IntPtr GetProcAddress(
IntPtr handle,
[MarshalAs(Unman agedType.LPStr)]string funcname);
************** *************

if I change it into unicode ,everything go wrong. I don't why ,maybe
GetProcAddress () only has ANSI version.

just put result here for someone may encounter the same problem.

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pam>
дÈëÏûÏ¢ÐÂÎÅ:e C************** @TK2MSFTNGP06.p hx.gbl...
>>>the getProcAddress( ..) doesn't work.Where am i wrong?

Does the filename of the DLL you need to load change after compile
time? If not, let p/invoke handle all the dynamic loading mess for
you and just use DllImport and declare the function you need to call.

-----------------------------------------

Ö Àñ£¡
Ryanivanka
sa********@gmai l.com

Jul 10 '08 #8
On Jul 10, 4:39*am, Ryanivanka <sarah.h...@gma il.comwrote:
I was told if I use LoadLibrary(..) to load a DLL, I have to freeLibrary(... ) by myself too,because CLR won't do that for me? is this correct?
Yes. CLR garbage collector only collects memory, everything else is
"unmanaged resources", and you have to take care of it yourself.
I use LoadLibrary(..) to control the bind of DLL by myself.And if I import my function using EntryPoint,woul d the DLL stay in the process's virtual address space until the process is ended?
Yes.
Jul 10 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7190
by: Carl Waldbieser | last post by:
I tried to adapt the instructions for building the M2Crypto module (http://sandbox.rulemaker.net/ngps/m2/INSTALL.html) to build a version compatible with Python2.3, but I've had some mixed results. I actually got everything to build and install, but when I try to import M2Crypto-- well, here is a sample session: >>> import M2Crypto Traceback (most recent call last): File "<interactive input>", line 1, in ? File...
1
1987
by: Jesper | last post by:
Hi, In visual studio 2003 I made an application that ran without problems on a number of 50+ machines (all running .Net 1.1). Now I've recompiled the program using Visual studio 2005, .Net 2.0 has been installed on all machines, completely updated from windowsupdate.com. On two mahines however theres a problem, I get this error: Cannot Access a disposed object. Object name: 'Button'.
0
1265
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file, then the datagrid is bound to it, and it works fine. The bottom DataGrid is bound to a different ArrayList that holds the same type as the first ArrayList. Both DataGrids are set to Read-Only. The user selects a row from the top DataGrid, then...
0
2068
by: Budhi Saputra Prasetya | last post by:
Hi, I still have the same problem with embedding Windows Control. I'll just requote what I posted last time: I managed to create a Windows Form Control and put it on my ASP .NET page. I have done the suggestion that is provided by modifying the security settings. From the stack trace, I would assume that the code throws exception when it
0
339
by: Michax | last post by:
Hi, I have problem with my py2exe. When I want to run my compiled exe, then i get error information like that: Trackback (most recent call last): File "mysql_gui.py", line 2 in ? File "gtk\__int__.pyc", line 12, in ? File "gtk\_gtk.pyc", line 12, in ?
3
3448
by: Rene | last post by:
Hello to all! For a long time I have been "fighting" a problem compiling an OpenGL program which uses GLUT. First I have put a question in a Watcom group (I want to use this compiler) to which I got no reply, in an OpenGL group somebody recommended me to use Visual C++ which I did. That worked OK but I do would like to use Watcom. In the meantime I found solutions to several of the errors I got but one is left which I cannot find a...
7
1965
pod
by: pod | last post by:
Hello My OS is Windows2000 I am new to .NET I developed a C# Windows Application that connects to a MS Access database on the network using proper UNC format. It works perfectly on my main machine (from which I have developed it).
91
3460
by: Eddie | last post by:
Hi I am using lcc-win on Windows 98. I'm writing a simple c console app, and I need to set the background color to blue. Here's the code I've got at the moment: _asm ( "movb $2, %ah\n" "movb $7, %dl\n" "int $0x21\n" );
2
3602
by: eliben | last post by:
On Jun 27, 3:10 pm, eliben <eli...@gmail.comwrote: Problem solved: http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/
0
1978
by: pion | last post by:
Hello I'm trying to make a web service client in python, and so to start out, I found this simple example that are supposed to parse an wsdl file using SOAPPy. I'm using Windows and got SOAPPy installed using the Enthought Python Distribution. I've tried several wsdl files and commenting out parts of the code etc. but I just can't twist my head around what's the problem here. Any help would be greatly appreciated. soappyTest.py: from...
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10240
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3100
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.