Well i wrote a small program and it seems Close statement in the above posted code will not give this error. There seems to be some other problem.

Not sure if you can step through the DLL. As your function (in the DLL) is not a big one, you can put simple Message Boxes after each statement and then you will be able to know which statement is actually erroring out.