Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
trying to call Find() from a test dll im messing with and I keep getting the error"Specified cast is not valid."

here's a little snippet of what im working with.....

test.dll
foo.h
C++
__declspec(dllexport) UINT Find();


foo.cpp
C++
UINT Find()
{
	return 1;
}


C# test.exe using interface for the call
C#
[ComImport, CoClass(typeof(object)), TypeIdentifier, CompilerGenerated, Guid("MYGUID")]
    public interface CSmart : ISmart
    {
    }

    [ComImport, TypeIdentifier, CompilerGenerated, Guid("MYGUID")]
    public interface ISmart
    {
        [DispId(1)]
        uint Find();
}


here is the button push
C#
private CSmart caller;
this.caller = (CSmart)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("MYGUID")));
           try
           {
               caller.Find(); //<---here is when the error pops
           }catch (Exception ex)
           {
               //Specified cast is not valid.
           }


Loading the dll into dependancy walker shows
C
1(0x0001) | 0(0x0000) | unsigned int Find(void) | 0x00011091


What I have tried:

not sure where to start....lol
Posted
Updated 10-Feb-16 9:14am
v3
Comments
Philippe Mori 12-Feb-16 20:16pm    
I don't understand your question. C/C++ code seems to be regular functions exported from the DLL while C# code seems to use COM object. You have to select one technology and used the same on both side. As such, that question does not make much sense.

1 solution

Your exported function doesn't look like a COM object to me. I'm not a great expert on C# but if all you've got is a C function can't you just call it directly after importing it:

C#
class urgle
{
    [DllImport("test.dll")] public static extern UINT Find();
};


is the sort of thing you need.
 
Share this answer
 
Comments
JEEPRFAM 10-Feb-16 17:48pm    
the test.dll has a bunch of other functions ex. DllCanUnloadNow, DllGetClassObject, DllInstall, DllRegisterServer, DllUnregisterServer ect.... to work with COM, I'm able to register it and it loads using Activator.CreateInstance im just having a hard time figuring out how to code the dll (not the best at c++) to work with the interface layout i have
<pre lang="c#">
[ComImport, TypeIdentifier, CompilerGenerated, Guid("MYGUID")]
public interface ISmart
{
[DispId(1)]
uint Find();
}
</pre>

so in other words to figure out what code is needed in the dll to work with that interface layout. if that makes any sense lol
Thanks for your response though
JEEPRFAM 11-Feb-16 11:48am    
we'll I think im getting closer now when calling Find() im getting this error -> Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

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