I am trying to figure out a way to check if a specific DLL / EXE file has security cookies enabled. I might say that I don't have the source code, and that I wasn't the one that compiled it. I have only the compiled DLL's and the executable.
I know that there is a way to achieve it by disassembling the file and check if the cookie was pushed to the stack before calling a function, and also check that before returning from a function a verification of that cookie is made. But, this method is really inconvenient and messy.
Does anyone know an alternative method for checking it?
Note : the code was written in C#/.NET, and I'm running Windows 7 64 bit.
You can guarantee that your C# code was not written with /GS because C# does not support /GS, that is a feature of the C++ compiler, the C# compiler does not have that option.
If you use the fixed statement in C# it is your responsibility to check for buffer overflows. Managed code will not let you have a buffer overflow.
So I guess you could say all "safe" code behaves like you had /GS enabled and all pointers used in "unsafe" code blocks behaves like it was not enabled.
Finding if a program was compiled with the compiler flag /unsafe is a different enough topic I would recommend asking a new question for that.
I had working code that implemented a wrapper and interface along the lines of:
public class wrapper : wrapperInterface {
...
}
Intellisense was generating a wrapperInterfacePtr so that in a C++ header I had a valid statement of:
wrapper::wrapperInterfacePtr m_wrapper;
I was mucking around trying to add some Delegate functionality so I could pass a C++ callback into the C# (I failed), and at some point during the mucking around the auto-generated wrapperInterfacePtr became undefined. Even after I returned all the code to (what appears) to be its original state, Intellisense is not generating wrapperInterfacePtr.
Any thoughts on how what I could have done, or how to kick-start Intellisense?
Ok, I got it to work again. I can't give the definitive reason it failed, but I have an idea. The new c# managed code is on top of a huge library of existing C++ unmanaged code. I build the managed code using VS and then use an in-house tool to build everything else. The in-house build tool doesn't do a clean unless you really, really force the issue. (It would take days.) When I did a build-clean on the dlls that interfaced with the managed code, that fixed the problem.
I am getting the "NotMarshalable recognized" error in my VB.NET application:
"A COM component which can not be marshaled is being using from a differrent apartement / context than the other it entered the CLR from. Because it can not be marshaled, it is correctly directly by the current apartement / context. This may lead to data loss."
The IDE stop in this line and offer "Get more information about MDAs" (which takes me to a website that explains MDAs). I have read through the website, but I did not find any information that help me explain which COM component is causing this error.
I can't read assembler, but I guess that all that I have here, right? ->
Can somebody tell me how to track down which COM component is causing this error and why? I have around 20 COM objects in my large project, and I can not rewrite all of them so quickly in .NET.
Thank you!
this is what I would try to do:
setup visual studio to stop on any exception
check what code is doing when the exception happen
check how the stack looks-like when the exception happen
use some .net decompiler to check what is happening inside the .net function that called the COM compenent
check the parameter of the function (by looking at the stack in VS) to try to understand which is the COM component
google any information found
This question already has an answer here:
0xC0020001: The string binding is invalid. - Only occurring in WPF
(1 answer)
Closed 9 years ago.
I am trying to find a way to debug my application but it is very difficulty for me. The error is when I close the program, sometimes it shows the error code as below:
Unhandled exception at 0x7537812f in Sample.exe: 0xC0020001: The
string binding is invalid
My application is a windows form written in C# in Visual studio 2012 Professional and the program uses some native functions from a dll which is written in C. I researched on the internet but almost all solutions are not using static variables or compile the dll with /clr in Visual Studio for C++, but my dll is C code from third party and it is built by MingGW so I can't follow these solutions. To change the static variables is impossible.
Please help me to find the solution for it?
Without code it's difficult to say what is the problem. But when you say, the crash happens (sometimes) during the application is closing, you probably can't provide the right piece of code easily.
The reasons could be, some code of the program uses some data, which is already destroyed.
If the internal implementation of your DLL is C++ e.g. a destructor could access data already deleted or created statically before the instance of the class. This would be mean, the DLL causes the crash internally. For that explicite calling of some kind of close API function could be the only help.
Another cause is possibly, you instantiate the DLL in a C# class, which is destroyed in garbage collection. As the instances are destroyed "uncontrolled" they can call the already unloaded DLL API. A flag signaling the validity of DLL API would help for that cause.
So my suggestion are:
Check, If your DLL provides an API function for closing or cleaning
up or unloading - call it and don't call any other API-function from
DLL after that. Use e.g. a global flag tor that, and access DLL only
if the flag allows it. (poor man's approach, better - ecapsulate the
DLL in a C# class)
Try to load and unload DLL in any test-function of your C# for making
the error easier reproducible.
Make sure, when your C# begins the closing process not to call any
DLL function any more (except the once calling clean/close/unload)
Hope it helps.
I want to integrate MATLAB Coder output with a C# project in Visual Studio 2010. My main idea is:
Create a *.m script in Matlab
Make sure the script is compatible with Matlab Coder.
Generate a C++ shared library (DLL) with Matlab Coder
Integrate with C# using something like this:
//Starts the model execution. May take several minutes
public static class DllHelper
{
[DllImport(#"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "Run()")]
public static extern int Run();
}
Also, I would like to be able to stop the execution and retrieve some partial results. To do this, I was thinking in two methods: StopExecution and RetrievePartialResults
[DllImport(#"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "StopExecution ()")]
public static extern int StopExecution ();
[DllImport(#"test.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "RetrievePartialResults()")]
public static extern MyResults RetrievePartialResults();
Is it possible to do? If no, is there any alternatives? If yes, where can I find more examples?
I have no idea if your plan works, but MATLAB Builder NE might be an alternative. It directly outputs a .Net dll without those hard limitations to the m-code.
The disadvantage is, that MCR is required on the target machine.
I've done both ways. Formerly, our project was using MATLAB Compiler, but we now switched to Coder, because that avoids the overhead of having to install the runtime (which btw often failed to start inside the process for no apparent reason).
We compile the coder output as an unmanaged C project with a C interface and use a C++/CLR project as a wrapper. This has the advantage that we don't need to manually specify the interface for P/Invoke as the compiler will directly read the header files. The C++/CLR assembly is the linked to the C# project where the code is going to be used. Be aware that this is kind of expensive, so try to avoid calling the matlab code in a tight loop and better move the whole loop into the library if that is possible.
Here's a snippet from the wrapper library (still uses old Managed C++ syntax, but that doesn't matter here)
bool CTurconConnect2::Init()
{
// Call the exported function in the library. Imported using a header file.
turcon_initialize();
// Call one of the matlab functions (in this case, the entry function is manually defined
// in the C library, to have a clean interface)
SetParameters(36.0,400.0,20.0,30.0,15.0,40.0,110.0, 0.0, 100.0);
return true;
}
bool CTurconConnect2::Exit()
{
turcon_terminate();
return true;
}
I think your plan of writing a DLL and calling it from c# seems to be one of the two main ways to go.
The alternative would be to:
use the MATLAB as an automation server from C# using the engine
interface via com automation. This allows you to simultaneously debug
your C# application from both the C# side and the MATLAB side, using
debuggers on each side.
Here are examples for both methods, and even a third alternate method (that seems less recommended).
Integrating MATLAB with C# on File Exchange
I've achieved the exact functionality you're asking about using the MATLAB Compiler. I don't have any experience with the MATLAB Coder, but should be the same principle. Once you have a MATLAB Library compiled, you can access it using P/Invoke in C# like you would with any other unmanaged library (and as you specified in your question).
Theres a couple of caveats:
I think you might have an issue design-wise trying to successfully implement your "stop execution" strategy. The MATLAB executables/libraries are meant to execute from start to finish without much control over the runtime. If you can split your script up into multiple pieces to handle that design, that may work better.
Compiled MATLAB libraries require you to "Start" and "Stop" the MATLAB Component Runtime manually, as well as a component runtime for each script. So, execution flow, would be something like:
StartMCL();
StartScript1_Runtime();
Run_Script1();
StopScript1_Runtime();
StopMCL();
If you attempt to run the "Script 1 Runtime" prior to starting the overall MCL, the app will crash. So, you need to be careful with how you design the wrapper class to handle that properly. Also, you want to be sure to Stop everything before exiting your app, otherwise, the MCR will effectively see 2 "Runs" in a row, and will crash.
You didn't cover any input/output arguments in your question, but in most cases, you will need to use MATLAB functions to create the MEX variables to hand data in/out of the MATLAB environment.
There is a great set of sample source here that should cover all of the above:
http://www.mathworks.com/matlabcentral/fileexchange/12987-integrating-matlab-with-c
Also, the Compiler help itself has a bunch of useful resources.
http://www.mathworks.com/help/compiler/shared-libraries.html
So in a list form,
I doubt that you will be able to stop the matlab code unless you break it up into multiple functions, which you may call as needed.
That you should be able to halt execution by calling on a thread and stopping the thread as you need, or better, send a signal for the thread to stop, which it will abort between functions (for the purpose of partial results)
That matlab is a terrible language for fulfilling the requirements on item 1 (not that I have ever had any good experiences with it myself)