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:37:56 -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:37:56 -0700 (PDT)   author:   Ryanivanka

Google
 
Web ureader.com


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