Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello Experts,

I have created a Native Dll (myNativeDll.dll) just to keep it simple it exposes one method.

__declspec(dllexport)char * GetStringFromDll( )
{

const size_t alloc_size = 128;
STRSAFE_LPSTR result = (STRSAFE_LPSTR)CoTaskMemAlloc( alloc_size );
STRSAFE_LPCSTR retstr = "This value is returned from native dll";
StringCchCopyA( result, alloc_size, retstr );
return (char *) result;
}

on the client side (Managed App) defined a helper class to use this dll as

C#
namespace MarshalStringApp
{
    public class MyDllWrapper
    {
        //  Calling Standard Windows Functions.
        [DllImport("User32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)]
        public static extern int MsgBox(int hWnd, String text, String caption, uint type);

        //  Calling Functions from our Dll.
        [DllImport("myNativeDll.dll")]
        public static extern String GetStringFromDll();
    }
}


and added a handler to the form to call this function.
C#
private void btnGetStringFromDll_Click(object sender, EventArgs e)
       {
           String str = "";
           try
           {
               str = MyDllWrapper.GetStringFromDll();
               MessageBox.Show("String Returned [" + str + "]");
           }
           catch (Exception eX)
           {
               MessageBox.Show(eX.Message);
           }
       }


I am getting an exception Code (0x8007007E), suggesting that DLL "myNativeDll.dll" is not found, i tried copying it to the client's folder to (where exe resides) but no avail. where should i copy this native dll, for this app to work. or i am missing something.

any help will be highly appreciated.

-prateek.
Posted
Updated 8-Jul-13 5:47am
v2
Comments
[no name] 8-Jul-13 11:11am    
Have you tried [DllImport("myNativeDll.dll")]?
Katiyar S. 8-Jul-13 11:50am    
Yes I have tried that only, DllImport("myNativeDll.dll")], extremely sorry, i made a mistake in dll name while typing on forum.

1 solution

Quickest solution is to put this dll in WINDOWS\SYSTEM32 folder and see whether it gets loaded or not.
 
Share this answer
 
Comments
Katiyar S. 9-Jul-13 4:19am    
No, i tried that too
_Asif_ 9-Jul-13 4:43am    
For debugging purpose hardcode the path of the dll. For example
http://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900