Implementation of a managed wrapper DLL
Is returning a nullptr in case of bad parameters okay?


No, you should instead throw an ArgumentException (or possibly one of its derived types). This way, if an user of your code makes a mistake, they will get an exception clearly explaining the problem, instead of getting a confusing NullReferenceException at a later point.


  What about handling error if any should occur when creating the native object Codegen?


Again, you should throw a suitable .Net exception. In this case, I think a custom exception would be appropriate.


  Should I just encompass this in a try/catch block and wrap a native exception inside a managed one?


Not necessarily. Unmanaged exceptions are represented as the managed SEHException. So, if that's enough for you, you don't have to do anything.

But if you want to be able to handle some specific unmanaged exceptions from the managed code, or if you want to preserve some information from the unmanaged exception (SEHException doesn't really do that), you should catch the unmanaged exception and throw a new managed exception. For example:

public ref class UnmanagedException : Exception
{
public:
    UnmanagedException(String^ message)
        : Exception(message)
    {
    }
};

…

try
{
    // call some code that can throw std::exception here
}
catch (const std::exception &amp;ex)
{
    throw gcnew UnmanagedException(gcnew String(ex.what()));
}