I got following errors when tried to calling .dll (made from delphi) function from unity3d C# code.
Here is pictures,
and errors says,
and .dll codes are,
So why error occurs and how to solve?
Thanks a lot!
Where you write
error = GetRequestResult(code);
you need to write
error = GetRequestResult(out code);
which is precisely what the second error message states.
Looking at your code, returning a PChar from the Delphi DLL the way that you do is not compatible with your P/invokes. The P/invoke marshaller is assuming that your return values were allocated with CoTaskMemAlloc and will call CoTaskMemFree on the pointer that you return. That's going to lead to some problems somewhere down the line. I think you'll need to tackle that issue at some point but since it's not the subject of this question, I won't attempt to solve the problem here.
Related
There are three different return/error/status code types I get while calling native function using P/Invoke.
The first one is the usual error code I get by calling Marshal.GetLastWin32Error static method.
The second one is HRESULT I get while calling some COM-helper functions.
And the third one is NTSTATUS I get while calling native apis from ntdll.dll library. I have a loging function that logs the return/error/status code as a hexidecimal number, then when an error is found in the logs I need to use google to find out what the actual error is.
So I'd like to ask if there is a good way to convert these return/error/status codes to string (preferably in English) to log the return/error/status string description instead of a number?
Is there a way to convert between LastError/HRESULT/NTSTATUS codes, so that the semantics of the error message would stay the same?
You can use errorlook tools for this purpose. This link is kind of old, I learned this in my old VC++ days and you can use it as a start point to find modern tools.
As for the logging I would suggest you keep the original error codes. Meaningful error messages are easier to read, but the original error codes are accurate keywords to the search engines.
I need to use a program made in C for a C# project , and for this I need to generate a DLL in Visual Studio 2013, anyone have any tutorial or know how to do it?
I want make a DLL for a structs and read/write functions in C, cheers guys.
You have to use p/invokes. I think this is very often discussed here. Take a look at: Access a method from a DLL from C# program
DLL is probably overkill for what you are doing. You can just write a C procedure and P/Invoke it using CDecl. The problem is that you need to know a whole lot for this. If you are simply trying to iterate over an array fast for a performance critical section, you are better off using structs in C#, slapping unsafe on your method, and then using pointers and addresses to do what you want to do. Code that looks a whole lot like C/C++ is perfectly legal in C#.
See a reference here: MSDN : Unsafe Pointers
Also fished out a somewhat dated reference showing how to P/Invoke the visual c Runtime printf function as an example. Keep in mind that things get really hard when you need to give a pointer to a function and when you need to read offsets etc. to pass around structs. You'll need to pin anything you pass into the method to stop it from being moved by the garbage collector, which will also have performance implications.
MSDN: CDecl P/Invoke example
I have a DLL file with four functions, I need to get access to the four functions but they don't have names, just entry points which I know. I have to figure out what the four functions do.
The question is, how do I call these unknown functions in C#(.Net)? And how can I figure out what their parameters and return values are?
The DLL is Sndvolsso.DLL . It's functions are nowhere on the internet.(unless you can find them) If anyone knows of a tool I can use to read parameters and return values, that would be very helpful.
If your DLL is unmanged:-
Using IDA
Disassemble the dll and find the exported functions.
Double click that function and its declaration will be shown in IDA.
An Example of MessageBoxA of user32.dll in IDA:-
.text:77D6EA11
.text:77D6EA11 ; int __stdcall MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
.text:77D6EA11
You can also use the free version of IDA 5.0.
Parameter / type / signature information is not stored in unmanaged DLL files. You can get the address of each exported function using LoadLibrary and GetProcAddress, and using ordinal. But all you get is the address; it's up to you to know how to call the functions appropriately.
It would help you to understand how functions work at the binary / machine code level and how the stack works.
Here are your options:
As you said, find information from external sources like the internet.
If you can find the PDB file, this is the debugging information file, and it's how debuggers know how to associate locations in memory to lines of code. It will contain all the metadata you're looking for. But in your scenario I don't think you have access to this.
If you are comfortable with assembly and understanding executable code at the binary level, disassemble or debug the DLL. See what it's expecting, how it uses the stack.
Otherwise you will have to compromise:
Use a different library. Most practical, because honestly if you don't know anything about this sndvolsso.dll, then even if you get it "working", you will be lost once any problems arise.
Hire someone who can do this.
Just write your own code instead of using this one.
But come to think of it, your question doesn't really make sense. If you don't know what these functions are, how do you know you need them?
I have a C# DLL that uses the XslCompiledTransform class for xml manipulations. I stole a C++/CLI wrapper for the C# DLL.
When using Delphi 5 to implement the C++/CLI wrapper, I receive a System Arithmetic error. Here is the Delphi 5 declaration:
procedure XsltMethod(XmlPath, XsltPath: PWideChar); cdecl; external 'ahma.dll';
The body of the C# public method creates a new XslCompiledTransform object and the exception pops up right when the newly created object runs its load method. For example:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XsltFile);
As mentioned earlier, the exception thrown from the .NET DLL is a System Arithmetic Exception. This only happens when called from a Delphi executable.
I guess I should mention calling the object's load method again works fine. So catching the exception and running the method for a second "pass" acts like a popup blocker. But for exceptions, of course.
Maybe you suffer from differences in the Floating-Point Control Register as stated here. Also see this QC report. You could try calling Set8087CW($133F); in your Delphi program. Be cautious of floating point problems in your Delphi code after that.
Random thoughts:
I think you should start by debugging your assembly from Visual Studio. Insert a messagebox or other wait statement in the Delphi code, then attach to process from Visual Studio. Tracing the C# might provide a couple of hints on what is going wrong. If you can't get it working, at least add logging of the incoming parameters.
In delphi, you don't need to escape backslashes.
Are you sure the E0434F4D is not some innocent first-chance exception? If you do not debug (or continue from the JIT debugger exception stop, which I'm not entirely sure is possible with Delphi 5), is the behaviour indeed faulty?
Could we please refer to "native Win32 assembly" as "DLL", like we used to call them for the past 20 years? :-)
I am trying to use Adobe IFilter to search PDF files. My code is written in C# and I am using p/invoke to get an instance of IFilter:
[DllImport("query.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private extern static int LoadIFilter(
string pwcsPath,
[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
ref IFilter ppIUnk);
This works for most PDF files except a few for which this method returns -2147467259 and ppIUnk is coming as null. Does anyone have this type of errors or have any suggestions of how to figure this out?
See the MSDN docs for the LoadIFilter function - you should be getting one of the error codes, at least according to that page.
E_ACCESSDENIED = 0x80070005 -
The function was denied access to the filter file.
E_HANDLE = 0x80070006 -
The function encountered an invalid handle, probably due to a low-memory situation.
E_INVALIDARG = 0x80070057 -
The function received an invalid parameter.
E_OUTOFMEMORY = 0x8007000E -
The function did not have sufficient memory or other resources to complete the operation.
E_FAIL = 0x80000008 -
The function encountered an unknown error.
(Also, the full set of constant values is listed here, which seems to be rather longer than that listed on MSDN.) Now, the interesting things is, your error code corresponds to 80004005 in hex, which isn't listed on either or those pages. I'm suspecting pinvoke.net may have it wrong however, as many other sites (this for example) list it as E_FAIL... not that it really helps anyway. Sorry for the inconclusive answer, but maybe it will point you along the right track at least.
Edit: This error seems to have been documented elsewhere and caused much confusion to many people, with no simple solution. It seems like the cause could be one of several in fact... There's various suggestions here and here that you may want to try, but I don't think I can help you any more than that, as I have never encountered this error myself in this context. Good luck anyway...
Here's how I solved it:
Uninstall Adobe Reader/Acrobat. Install the latest version (again). It should fix the pdf filters. Good luck.
To get around the E_FAIL for Adobe 10.x, please see https://stackoverflow.com/a/8841476/455322
I got the same result when running LoadIFilter until I found this post which pointed me to the solution:
Make sure that your build platform target is x86 and avoid running the application from within Visual Studio.
Also, if you can move your code that interacts with the IFilter into a separate .exe application with the name filtdump.exe, you will simplify your code considerably.