Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Tue, 8 Jul 2008 07:34:51 -0700 (PDT),    group: microsoft.public.dotnet.languages.csharp        back       


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(dllexport) WCHAR*  __stdcall getWNameOfComponnet();

}

WCHAR name[100];

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
    return TRUE;
}


WCHAR*  getWNameOfComponnet(){
	return name;
}

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

EXPORT
getWNameOfComponnet  @1
*****************************************************


and I try to use the export in c#,but I can not get anything from
getprocaddress(,.)

**************import the prototype of api**
[DllImport ("kernel32.dll",EntryPoint="LoadLibraryW",
CharSet=CharSet.Unicode,CallingConvention=CallingConvention.StdCall,
SetLastError =true)]
public extern static IntPtr   LoadLibrary(String  lpFileName);

[DllImport("Kernel32", EntryPoint = "GetProcAddress",
CharSet=CharSet.Unicode ,CallingConvention =
CallingConvention.StdCall,
SetLastError = true)]
 public extern static IntPtr  GetProcAddress(
            IntPtr handle,
            [MarshalAs(UnmanagedType.LPWStr)]string funcname);

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

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

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

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

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

        }

********************************


the getProcAddress(..) doesn't work.Where am i wrong?
date: Tue, 8 Jul 2008 07:34:51 -0700 (PDT)   author:   Ryanivanka

Re: problem in using win32 DLL in c#   
> 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.
date: Tue, 8 Jul 2008 11:10:51 -0500   author:   Ben Voigt [C++ MVP] am

Re: problem in using win32 DLL in c#   
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("Kernel32.dll", EntryPoint = "GetProcAddress", CharSet = 
CharSet.Ansi, CallingConvention = CallingConvention.StdCall , SetLastError = 
true)]
        public extern static IntPtr  GetProcAddress(
            IntPtr handle,
            [MarshalAs(UnmanagedType.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]" <rbv@nospam.nospam> дÈëÏûÏ¢ÐÂÎÅ:eCwjTSR4IHA.1420@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.
>
date: Wed, 9 Jul 2008 10:48:39 +0800   author:   Ryanivanka

Re: problem in using win32 DLL in c#   
> **************import the prototype of api**
> [DllImport ("kernel32.dll",EntryPoint="LoadLibraryW",
> CharSet=CharSet.Unicode,CallingConvention=CallingConvention.StdCall,
> SetLastError =true)]
> public extern static IntPtr   LoadLibrary(String  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("myfile.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"  wrote in message 
news:%23BH0N5W4IHA.2332@TK2MSFTNGP03.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("Kernel32.dll", EntryPoint = "GetProcAddress", CharSet = 
> CharSet.Ansi, CallingConvention = CallingConvention.StdCall , SetLastError 
> = true)]
>        public extern static IntPtr  GetProcAddress(
>            IntPtr handle,
>            [MarshalAs(UnmanagedType.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]" <rbv@nospam.nospam> 
> дÈëÏûÏ¢ÐÂÎÅ:eCwjTSR4IHA.1420@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.
>>
>
>
date: Wed, 9 Jul 2008 01:07:22 -0400   author:   Jeff Winn

Re: problem in using win32 DLL in c#   
On Jul 8, 6:34 pm, Ryanivanka  wrote:

> the getProcAddress(..) doesn't work.Where am i wrong?

Here:

> [DllImport("Kernel32", EntryPoint = "GetProcAddress",
> CharSet=CharSet.Unicode ,CallingConvention =
> CallingConvention.StdCall,
> SetLastError = true)]

You either need to use CharSet=CharSet.Auto, or
EntryPoint="GetProcAddressW". If you specify CharSet explicitly, P/
Invoke won't auto-decorate the function name.
date: Wed, 9 Jul 2008 07:20:02 -0700 (PDT)   author:   Pavel Minaev

Re: problem in using win32 DLL in c#   
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("Kernel32.dll", EntryPoint = "GetProcAddress",
> CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall ,
> SetLastError = true)]
>        public extern static IntPtr  GetProcAddress(
>            IntPtr handle,
>            [MarshalAs(UnmanagedType.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]" <rbv@nospam.nospam>
> дÈëÏûÏ¢ÐÂÎÅ:eCwjTSR4IHA.1420@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.
date: Wed, 9 Jul 2008 11:21:38 -0500   author:   Ben Voigt [C++ MVP] am

Re: problem in using win32 DLL in c#   
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  wrote:
>
>> the getProcAddress(..) doesn't work.Where am i wrong?
>
>Here:
>
>> [DllImport("Kernel32", EntryPoint = "GetProcAddress",
>> CharSet=CharSet.Unicode ,CallingConvention =
>> CallingConvention.StdCall,
>> SetLastError = true)]
>
>You either need to use CharSet=CharSet.Auto, or
>EntryPoint="GetProcAddressW". If you specify CharSet explicitly, P/
>Invoke won't auto-decorate the function name.

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

  Ö Àñ£¡
             Ryanivanka
             sarah.hhyy@gmail.com
date: Thu, 10 Jul 2008 08:32:15 +0800   author:   Ryanivanka

Re: problem in using win32 DLL in c#   
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,would 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 
>GetProcAddress for you.
>
>>
>>
>> and I change the prototype of getProcAddress(..) in C# as follows:
>> **************************
>>        [DllImport("Kernel32.dll", EntryPoint = "GetProcAddress",
>> CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall ,
>> SetLastError = true)]
>>        public extern static IntPtr  GetProcAddress(
>>            IntPtr handle,
>>            [MarshalAs(UnmanagedType.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]" <rbv@nospam.nospam>
>> дÈëÏûÏ¢ÐÂÎÅ:eCwjTSR4IHA.1420@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. 
>
>

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

  Ö Àñ£¡
             Ryanivanka
             sarah.hhyy@gmail.com
date: Thu, 10 Jul 2008 08:39:30 +0800   author:   Ryanivanka

Re: problem in using win32 DLL in c#   
On Jul 10, 4:39 am, Ryanivanka  wrote:

> 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,would the DLL stay in the process's virtual address space until the process is ended?

Yes.
date: Thu, 10 Jul 2008 05:09:26 -0700 (PDT)   author:   Pavel Minaev

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us